Hi guys!
I'm trying to access a remote database (MySQL) that is installed on my home computer. The code is:
public void initDB(String ipaddress, String databasename, String login, String pass) {
try {
Class.forName("com.mysql.jdbc.Driver");
db = (Connection) DriverManager.getConnection("jdbc:mysql://" + ipaddress + "/" + databasename, login, pass);
statement = (Statement) db.createStatement();
System.out.println("Database is connected.");
} catch (Exception e) {
System.out.println("Could not initialize the database.");
e.printStackTrace();
}
}
This code works perfectly with the local database (when the code is executed from my home computer). Now I'm trying to run the code from another computer, and I cannot connect to the database. Where could be the problem?
Some details about the database: I'm using Wamp server. To allow a remote access, I've opened the file phpmyadmin.conf in C:\WAMP\ALIAS and replaced Allow statement with Allow from all. Also, I've created new user and I've given the value of % for the Host. That is all I've done. Maybe, I missed something, don't know...
Below you may see an error message:
Could not initialize the database.
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:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1118)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:343)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2308)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2122)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:774)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:375)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:289)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at SystClasses.QueryTableModel.initDB(QueryTableModel.java:88)
at SystClasses.QueryTableModel.<init>(QueryTableModel.java:36)
at SystClasses.FilterClass.initComponents(FilterClass.java:67)
at SystClasses.FilterClass.<init>(FilterClass.java:51)
at SystClasses.Form.createContacts(Form.java:280)
at SystClasses.Form.createBaseRight(Form.java:196)
at SystClasses.Form.createGUIforAdmin(Form.java:111)
at SystClasses.Form.<init>(Form.java:84)
at SystClasses.Form.main(Form.java:356)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
null
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:218)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:292)
... 22 more
Please, help me with some advise. Thanks!