简体   繁体   中英

can not connect to remote mysql server from java application

I created java application. It works on localhost. But can't connect to remote server. here is my code

    public Connection getDBConnection() throws Exception {
    Connection conn = null;
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    conn = DriverManager.getConnection("jdbc:mysql://192.168.0.101:8081/mydb", "user", "password");
    return conn;
}

i using jdbc driver.

but i recieved error msg

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.

How can i fix this problem.

    try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
        "databaseName=dbname;user=1111;password=1111;";
    Connection con = DriverManager.getConnection(connectionUrl);
    } catch (SQLException e) {
        System.out.println("SQL Exception: "+ e.toString());
    } catch (ClassNotFoundException cE) {
        System.out.println("Class Not Found Exception: "+ cE.toString());
    }

change your database name and user and the password and there you go

I think the server may not be configured to accept requests from your public IP address. For example, in cPanel you need to "allow" access to MySQL from specific IP address. By default only localhost is allowed. You may need to contact the server admin and to request your IP to be allowed to access the MySQL server.

Your is definitely not an authentication issue as otherwise you would have received authentication failure response.

You can also try to connect to your server from any MySQL client application eg MySQL Workbench, SQLYog etc. to test the issue.

Also check the MySQL server IP address / name in your connection string to be correct as we cant confirm it here. Check it with your Server admin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM