简体   繁体   中英

Connect to mysql remote database using JDBC?

I'm a java beginner and I'm working on a simple application which connects to a remote mysql database using JDBC. I've tested it locally and it works just fine, however I cannot get it to work on for my remote server. I don't think its of much use but heres the code:

Connection connection = null;
String dburl = "jdbc:mysql://314159265:3306/Db_Name";
String userName = "user";
String passWord = "password";

    try {
        Class.forName("com.mysql.jdbc.Driver");

        connection = DriverManager.getConnection(dburl, userName, passWord);
        Statement st = connection.createStatement();                                 

        String query = "INSERT INTO Example (`TestColumn`) VALUES('hello')";
        int rsI = st.executeUpdate(query);
        System.out.println("Hi");
        }catch (Exception e) {
        System.out.println(e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
                System.out.println("Database connection terminated");
            } catch (Exception e) { /* ignore close errors */ }
        }
    }

When I run this, I get the following message:

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.

I'm pretty sure it must be some kind of server configuration issue.

 Notes:
 Username, password, IP, database name, etc. are just examples.
  1. This could be a firewall problem, or a configuration problem. But I don't think it is a coding problem at all - you need to start troubleshooting the connection.

  2. Trouble shoot by attempting to use third party client apps to connect to mysql. This will indicate whether it is configured for external access. Although it doesn't ensure that JDBC is visible from the outside, it does rule out some potential firewall problems.

  3. Follow this guide to help you mess with your configurations

Remote MYSQL Database Access

If you are still stuck, it could be a coding problem so check out this page:

How to connent to a remote mysql database with java?

PS I am assuming you are using unix as the operating system.

我猜314159265可以被某个地址替换....比如jdbc:mysql:// localhost:3306 /或jdbc:mysql://127.0.0.1:3306 /

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