I got a google cloud platform - compute engine instance, which I installed MySQL server on.
And now I can't get any signal of life our of the VM the sql installed on,
for exsample:
package com.company;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
public static void connection(){
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("in conncection");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void connectToMySQL(){
connection();
String host = "jdbc:mysql://hotsIP:3306/DBname";
String user = "user";
String pass = "password";
try {
Connection connection = DriverManager.getConnection(host,user,pass);
System.out.println("???");
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
connectToMySQL();
}
}
It's take a few second like he trying to connect and the EXEPTION
in conncection
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
What I done to make it work:
in the my.conf:bind address = 0.0.0.0, skip-external-locking comment out
restart the server
looked if the server is active
looked if the server listening to the port
looked if its TCP
I don't know what to do anymore.
You have to make the following change to your my.cnf file
my.cnf
bind-address = www.000webhost.com (OR)
bind-address = xx.xx.xx.xx (IP Address)
You need to restart your MySQL service, once this setting is changed.
Also worth noting is the point that MAMP/ MAMP Pro sets MAMP_skip-networking_MAMP by default. You've to disable this line in your my.cnf
And if you don't have any user login issues, you should be able to connect to the MySQL Database from your Java code.
In my case the root cause was: Firewall. I was trying to run the application at work.
What was interesting is that the App Engine Standard running locally actually generated a non-error log in Google Cloud Platform Logs, making me discard the firewall hypotheses.
Solution: I found out bringing my notebook from home and connecting to company's network, did not work. When I connected to the shared connection in my mobile, worked perfectly.
Related
A few classmates and I are creating a Java project which requires a database. I have created a connection in MySQL and connected it to my Java project successfully using the following Connect class:
package com.example.javaworkoutgame.Model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connect {
static Connection con;
public Connect() {
connect();
}
// attempt to connect to MySQL database
public static void connect() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("Driver Loaded Successfully");
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lab3", "root",
"**********"); // not the actual password
System.out.println("Successful Connection");
} catch (ClassNotFoundException cnfe) {
System.err.println(cnfe);
} catch (SQLException sqle) {
System.err.println(sqle);
}
}
}
This code runs properly on my machine.
I committed and pushed the code to Bitbucket so my partners could access it. However, when they run the code on their computers, they get the following error message:
java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
Is there something I need to change in MySQL workbench in order for other people to be able to access the database? I could not find any information on this.
The only thing I was able to try was found at this thread:
java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
I opened a new .sql file and tried running the command:
GRANT ALL PRIVILEGES ON . TO 'root'#'localhost' IDENTIFIED BY '%password%' WITH GRANT OPTION;
(I replaced '%password%' with the actual password)
When I tried that I got the following error message:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY '*********' WITH GRANT OPTION'
No, and you need to stop this line of thought and do some research first.
Your current configuration says that the mysql server is on the very same physical machine that the code is running on. You installed mysql on your dev machine, your friends need to install it on theirs, and each has their own unique database (nothing is shared).
You could, instead, take your mysql server, open it up to the world (which, for virtually all ways internet is made available in residential connections, requires messing with your router to 'open a port').
But then you have an open mysql server, and the username and password are on a public bitbucket site.
It also requires either a permanent IP (which few residential internet providers offer) or a dyndns service. More generally, hosting open MySQL servers that see lots of traffic gets your internet shut down, for good reason. You'd end up hosting a whole bunch of hackers. All hardware in your network will be p0wned and turned into bot nets. Hence, very very bad idea.
Good ways to solve this problem:
Everybody installs their own MySQL server. This is sensible; you're writing code and bound to make mistakes, it'd be real bad if all code you write is first-run and tested on live data. You don't want one of your friends to wipe your database. If you need some initial data to test with, set it up properly, and read up on how to make an SQL dump. With such a dump file you can reset any mysql server to that exact state - and that'd be how you and your friends develop: Set up the DB to be in that known state, write some code, and if you ruin the db by doing so, no problem. Just reset it again.
Set up a VPN between your friends. NOW you can share the IP your system has within the VPN (it'll be 10., 172.16., 192.168.* - if it's 127.0.0.1, it's localhost, i.e. everybody needs to install mysql on their own and nothing is shared, and if it's anything else, you're opening it to the world, which you don't want to do). Do not put the VPN username/password info anywhere in that bitbucket. And you need to trust your friends.
You should have a properties type file so that each person who is going to interact with the code has their local data without the need to replicate yours, in the same way you can have different values in the properties for test or production environments.
example of a property file:
system.properties
#BD
db.driver=com.mysql.cj.jdbc.Driver
db.user=user
db.pass=password
db.server=server_IP
db.port= port_IP
db.db = DB
Then you should have a procedure to read from java the properties inside the file
Utils.java
package com.example.javaworkoutgame.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public final class Utils {
public static Properties getProperties() {
String path = String.format("PATH to your properties FILE/system.properties",
System.getProperty("user.dir"));
Properties properties = new Properties();
try (InputStream is = new FileInputStream(new File(path))) {
properties.load(is);
} catch (IOException e) {
throw new IllegalStateException(e);
}
return properties;
}
}
And finally you make a call to the function that gets the properties from your connection class
Connect.java
package com.example.javaworkoutgame.Model;
import com.example.javaworkoutgame.util.Utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connect {
Properties properties = Utils.getProperties();
static Connection con;
public Connect() {
connect();
}
// attempt to connect to MySQL database
public static void connect() {
try {
String driver = properties.getProperty("db.driver");
String ip = properties.getProperty("db.ip");
String port = properties.getProperty("db.port");
String db = properties.getProperty("db.db");
String user = properties.getProperty("db.user");
String pass = properties.getProperty("db.pass"):
Class.forName(driver);
System.out.println("Driver Loaded Successfully");
con = DriverManager.getConnection("jdbc:mysql://"+ip+":"+port+"/"+db, user,
pass);
System.out.println("Successful Connection");
} catch (ClassNotFoundException cnfe) {
System.err.println(cnfe);
} catch (SQLException sqle) {
System.err.println(sqle);
}
}
}
About the MYSQL error, if your partners do not have a local mysql environment with the same values as you, they will experience the error you describe, since your configuration is a local configuration, if you need your partners to connect to your pc, you must open the ports of mysql and give them your public IP (not recommended)
I hope this answer helps you!
I am creating a program for a local organization to take inventory. App connects to SQL Server via MSSQL 8.2 connector. App on phone should allow for receiving and sending item information. I have a static class to connect to the database, that handles the connection. In Eclipse, I am able to successfully connect and query the database. In Android studio, I get errors on every attempt to connect.
-Server is hosted on same pc.
-Ports have been configured, repeatedly. Currently have inbound and outbound rules for TCP and UDP fully open. Dynamic ports are on for SQL server. Services for SQL server have been restarted dozens of times. 1433 has been enabled and toggled off, but are currently blank.
-Attempted to connect by IP address, host cannot be resolved
-Permissions edited to allow login access to modify the database
-Integrated security and TLS security tried at different times, both failed.
List of errors:
Connection to the host BLACK-G, "xxxxx" port 1433, has failed.
TCP/IP connection to the host failed, unable to resolve host.
Connection to the host named BLACK-G instance sqlexpress failed,No
address associated with hostname, open up UDP traffic to port 1434
Unknown host exception. Unable to resolve host BLACK-G
Connection Refused
Failed to connect
Connection to the host 192.168.1.219 has failed. Failed to connect to
192.168.1.219 (port 1433) from 192.168.1.87(port 33654) after 2478ms Verify connection properties.
Here is the code
package com.example.ccupboard_1;
import android.os.Build;
import androidx.annotation.RequiresApi;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DatabaseJAVA {
// #RequiresApi(api = Build.VERSION_CODES.KITKAT)//public static void main(String[] args) {
public static String Connect() {
// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver://BLACK-G\\SQLEXPRESS;user=ay7;password=343434;databaseName=ayTestDatabase2;" ;
ResultSet resultSet = null;
try (Connection connection = DriverManager.getConnection(connectionUrl);
Statement statement = connection.createStatement();) {
// Create and execute a SELECT SQL statement.
String selectSql = "SELECT Fname, Lname from dbo.Customers";
resultSet = statement.executeQuery(selectSql);
// Print results from select statement
while (resultSet.next()) {
result += (resultSet.getString("Fname") + " " + resultSet.getString("Lname")+"\n");
}
} catch (SQLException e) {
e.printStackTrace();
result = e + "";
}
return result;
}
}
Snippet from main activity that calls connect method:
case R.id.buttonSignOut: { //used to test connection currently, simply attempts to connect and //returns the value to a textview on the page
Thread thread = new Thread() {
public void run() {
textviewLowItemAlert.setText(DatabaseJAVA.Connect());
}
};
thread.start();
break;
What would be the next step in resolving this issue? It works in Eclipse, but not in studio. I've tried more than a dozen different connection Strings, and most of them threw the same errors. Does my server not allow access to Android Studio? Most of the errors seem to be Android studio being incapable of locating it on the server.
Here's a snippet from a working app of mine where I can connect succesfully to a local ms sql.
In a config file I set the connection info.
public static String dbUser = "user";
public static String dbPassword = "password";
public final static String sqlString = "jdbc:jtds:sqlserver://ip:port//INSTANCENAME";
Then I start the connection like this.
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
DriverManager.setLoginTimeout(5);
connection = DriverManager.getConnection(Config.sqlString, Config.dbUser, Config.dbPassword); // Connect to database
if (connection == null) {
ConnectionResult = "Verify Internet Connection";
Toast.makeText(activity, ConnectionResult, Toast.LENGTH_LONG).show();
Finally just do the query and close the connection.
Alright, was finally able to figure it out, with the JDBC-JTDS connector mentioned by JoaquinAlvarez.
Here's what happened:
-Used the connection info he provided, with my IP address, and a higher timeout
-Network Error IOException, EHOSTUNREACH(No route to host),
-Router changed my IP address, so had to fix that
-After that, I used my PCNAME\SQLEXPRESS for the the for the instance
-It returned Instance Unreachable, so I changed to SQLEXPRESS alone
-Then it returned DB Closed connection
-I looked at Windows Event Viewer, and it said:
"Encryption is required to connect to this server but the client library does not support encryption; the connection has been closed. Please upgrade your client library."
-I checked the SQL server in SQL SERVER Configuration Manager... Force Encryption was turned on.
-Tried again, and the JTDS connector finally connected to the Database and I was able to do things. No help with the MSSQL one yet though.
Thank you Mr. Alvarez!
I'm working on a personal project for school where I have to user RMI to communicate between server and client.
Project info
The goal of my project is to retrieve stock info (from NYSE) for each day on the server at a specific time (after NYSE is closed). Each stock object is saved in a database. The information is retrieved over http and has nothing to do with RMI.
For the client it is also possible to fetch the stocks. When a user wants to fetch the stock object for the current day, it is directly fetched from the 3th party service. When a user, for example, wants to fetch Google's stock from last month, it is requested on the server over RMI. The server will the look for the stock object in the database and retrieve a Stock object and send it to the client.
Problem
When I start the client application, I have to login. The client will create a User object containing the username and password.
When I press the login button, it will take around 2 minutes before the main screen will be shown.
Below the source code where I setup the RMI connection.
Server (main.java)
public static void main(String[] args) throws UnknownHostException {
InetAddress IP= InetAddress.getLocalHost();
System.out.println("IP of my system is := "+IP.getHostAddress());
if(args.length == 1 && args[0].toLowerCase().equals("local")) {
System.out.println("Running on localhost");
System.setProperty("java.rmi.server.hostname", IP.getHostAddress());
} else {
System.out.println("rmi hostname is set to 37.97.223.70");
System.setProperty("java.rmi.server.hostname", "37.97.223.70");
}
try {
Registry reg = LocateRegistry.createRegistry(1099);
StockAppServer server = StockAppServer.getInstance();
reg.rebind("StockApp", server);
System.out.println("StockApp bound for StockAppServer object.");
} catch (RemoteException e) {
e.printStackTrace();
}
}
Based on the arguments that are passed to the application when it starts, I set the RMI hostname to my current IP address, or to the remote server address. The remote server address is a static IP, so this won't change.
Server (StockAppServer.java)
This class implements the interfaces that is used by the client to call methods on the server. So this class extends UnicastRemoteObject. When I start the server, registerStockTask() will be called. This method will fetch the ticker symbols (What are ticker symbols?) and then schedule a task to fetch all stock objects at a specific time.
private static StockAppServer _instance;
private List<User> loggedInUsers;
private List<Group> activeGroups;
private List<Notification> registeredNotifications;
private StockAppServer() throws IOException {
_instance = this;
this.loggedInUsers = new ArrayList<>();
this.activeGroups = new ArrayList<>();
this.registeredNotifications = new ArrayList<>();
this.registerStockTask();
clearActiveGroups();
checkForCompletedNotifications();
// Start the restful framework to allow incoming connections from the NodeJS server to manage new notification
Router.getInstance();
}
public static StockAppServer getInstance() {
try{
return _instance == null ? new StockAppServer() : _instance;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Client (main.java)
public static void main(String[] arguments) throws Exception {
args = arguments;
Application.launch();
}
#Override
public void start(Stage primaryStage) throws Exception {
InetAddress IP= InetAddress.getLocalHost();
System.out.println("IP of my system is := "+IP.getHostAddress());
if(args.length == 1 && args[0].toLowerCase().equals("local")) {
// Program started with local command, expect that server is running on local host
reg = LocateRegistry.getRegistry(IP.getHostAddress(), 1099);
System.out.println("Attempting to connect to RMI server over 127.0.0.1");
} else {
// Program started without additional commands. Except that "the server" is available;
reg = LocateRegistry.getRegistry("37.97.223.70", 1099);
System.out.println("Attempting to connect to RMI server over 37.97.223.70");
}
try {
StockApp.getInstance().setServerInterfaces((IStockSend) reg.lookup("StockApp"), (IUserHandling) reg.lookup("StockApp"));
} catch(RemoteException e) {
AlertMessage.showException("Unable to connect to server.", e);
} catch (NotBoundException e) {
AlertMessage.showException("No server has been found with the name \"StockApp\" on the remote host.\nPlease try again later", e);
}
LoginController.showMenu();
//FileNotFoundException e = new FileNotFoundException("Couldn't find file blabla.txt");
//AlertMessage.showException("Something went wrong. Please try again later.", e);
}
How I tried to solve my problem
When I test my applications local, there is no problem. The login method will be finished within a few milliseconds and I will be represented the main screen.
I started by turning of my firewall on my macbook. No result, login method still takes around 2 seconds.
I turned off the firewall om my Ubuntu server. No result, both firewalls on server and macbook are turned off. Login method still takes around 2 seconds.
On the server runs (thanks to jenkins) another (unrelated) program. This program uses sockets instead of RMI. When this program is not running, the login method still takes around 2 minutes.
In StockAppServer.java, I called the following method:
super(1099);
This has the same outcome as the above steps I took.
I don't know what else I can try to solve my problem.
I tried to give as much code as possible for the RMI part. I you need any other source code, just ask and I can update this question. Also, the source code is available via github: https://github.com/juleskreutzer/GSO-Maatwerk. Make sure to run the program with -remote param.
Update 1 (9-1-2017)
As yanys requested in the comments, I should run the following command:
dscacheutil -q host -a name localhost
this returns the following output:
Mac:
name: localhost
ip_address: 127.0.0.1
Ubuntu:
dscacheutil: command not found
Update 2 (9-1-2017)
I checked with the provider of my VPS where I run the java server on. On their side everything should be OK. According to them, it shouldn't be a dns problem. After some research, I found out that RMI uses both DNS and reverse DNS. It this case, reverse DNS was the issue. Please see my answer on how I solved my problem.
As EJP pointed out in the comments on the question, it was an DNS problem.
I contacted the support of my hosting provider to see if I had some wrong settings. They helped me a lot in solving this problem.
First we tested the speed of my VPS, this is around 1000mbit download and upload speed. After we checked this, they said there was nothing wrong on their side.
After doing some research, I found out that RMI uses both DNS and Reverse DNS. The problem was that I didn't setup the reverse DNS on my server. I already have a domain name to use for reverse DNS.
I than did the following:
Create a A-record on my website that points to the IP address of the server. I named it vps.mydomain.com
Add the reverse DNS in the control panel of my server
Change the hostname of my server to vps.mydomain.com*
*My server runs Ubuntu 16.04, on ubuntu machines with systemd, you can use the command
sudo hostnamectl set-hostname new-name
to change the hostname
I am trying to write a program in Java that in order to work, needs to have access to a MySQL database. Below is the code for the program so far, with the I.P. address, username, and password removed for security reasons. The problem with this code is that whenever it is run, it always fails to connect to the server, even though I know that it is running and that the password that the login information is correct. My friend found a program online that checks to see if your database can be connected to, and whenever he runs it, it always outputs "Where is your MySQL JDBC Driver?" What is the MySQL JDBC driver? I am assuming that it is the cause of my problem, but I don't know that for sure. Can anyone explain this to me?
import java.sql.*;
public class main
{
public static void main(String[] args)
{
// Store the information to connect to the MySQL server in handy variables.
String url = "jdbc:mysql://(IP REMOVED FOR SAFETY):3307/";
String dbName = "attendance";
String driver = "com.mysql.jdbc.Driver";
String userName = "(USERNAME REMOVED FOR SAFETY)";
String password = "(PASSWORD REMOVED FOR SAFETY)";
// Now let's connect!
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
} catch (Exception e) {
System.out.println("Could not connect to database!");
}
}
}
The problem could be that MySQL driver is not in your classpath.
Please look at this: http://dev.mysql.com/doc/connector-j/en/connector-j-installing-classpath.html
The MySQL JDBC driver is called MySQL Connector/J. This jar needs to be added to the classpath for your program to run.
The driver can be downloaded from: http://dev.mysql.com/downloads/connector/j/
I have downloaded JDK 6 and also I have sqljdb4.jar and I have database.properties file that content the following data
database.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
database.url=jdbc:sqlserver://.;databaseName=UserInfo;integratedSecurity=true;
database.username=sa
database.password=admin
B.N : I'm installing the server on my machine and the server name = . , also I'm using Windows Authontication
My problem now is when I try to create connection I have the following error
com.microsoft.sqlserver.jdbc.SQLServerException:
The TCP/IP connection to the host
localhost, port 1433 has failed.
Error: Connection refused: connect.
Please verify the connection
properties and check that a SQL Server
instance is running on the host and
accepting TCP/IP connections at the
port, and that no firewall is blocking
TCP connections to the port. at
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)
I don't know what is the exact problem here
If any one can help I will be appreciated
Thanks in Advance
That's caused by many probabilities like
1- IP is worong
2- Port is wrong
3- There is firewall prevent machine to go out and connect to another IP
4- SQL server down .
try to use
public class JdbcSQLServerDriverUrlExample
{
public static void main(String[] args)
{
Connection connection = null;
try
{
// the sql server driver string
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// the sql server url
String url = "jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE";
// get the sql server database connection
connection = DriverManager.getConnection(url,"THE_USER", "THE_PASSWORD");
// now do whatever you want to do with the connection
// ...
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
System.exit(1);
}
catch (SQLException e)
{
e.printStackTrace();
System.exit(2);
}
}
}
What i need to explain is there is very good technology called " Persistence " is better than JDBC and is more than brilliant and easy to use .
The problem is that your SQL server is either
not installed,
not running or
not accepting TCP/IP connections.
Particularly the last one is nasty, as I remember that some versions of SQL Server have not configured the TCP/IP connector to run by default.
Well first and foremost we need to see your code. Second looking at the error message the database is A)not running
B) on a different port
or C) the code is incorrect.