On my host computer I have installed Wamp and I can connect using PHP for example, or the browser. My client is a virtual machine with Linux and I am trying to connect to the database on the host to add some entries there.
Here is my code:
Connection conn = null;
try {
String url = "jdbc:mysql://192.168.1.236:8080/fps";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(url, "username", "password");
System.out.println("Database connection established");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
System.out.println("Database connection terminated");
} catch (Exception e) {}
}
}
I must mention that I'm trying to connect over a local network, and also that I setup Wamp to work on port 8080 (it can be seen above). Another thing is that, on the virtual machine, I can access the database using a browser.
The code above generates an error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at com.mysql.jdbc.Util.handleNewInstance(Util.java:400) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038) at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:628) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1014) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2249) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2280) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2079) at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:794) at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:44) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at com.mysql.jdbc.Util.handleNewInstance(Util.java:400) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325) at java.sql.DriverManager.getConnection(DriverManager.java:571) at java.sql.DriverManager.getConnection(DriverManager.java:215) at DatabaseConnection.main(DatabaseConnection.java:12) Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost. at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2926) at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:560) ... 16 more
I believe the problem could be from the settings of mysql on the client, in the file /etc/mysql/my.cnf but I don't really know how to change them since I didn't find an example for this particular situation. I also thought that the fact that I changed the port to 8080, when mysql normally uses 3306, could be causing problems.
So, the question is, how could I make this work? Thanks.
Edit: I also want to add that the port change (to 8080) was made in the conf file of apache on the server. This by default is 80. So maybe it shouldn't affect the 3306 of mysql.
mysql
running on 8080 port. – Sridhar