1

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.
10
  • 2
    Just a guess here, but is your host really 314159265? Should that be some sort of IP or is that really a DNS name? Commented Oct 30, 2012 at 23:04
  • Do you have to set up some kind of ssh/ssl tunnel to the remote server before accessing the driver? Also - can you access the database using a remote query browser on a client system? From memory I think you have to configure it so that the database is exposed, by default I am pretty sure databases are only available on local host so when you connect to the JDBC you will be getting rejected. I bet your java code works on the same server as the database Commented Oct 30, 2012 at 23:07
  • Can you reach that machine 31415... from your machine (where you're running that program from)? Try to ping that "address" or telnet into it on port 3306 Commented Oct 30, 2012 at 23:07
  • did u try st.execute(querry); instead of int rsI = st.executeUpdate(query); Commented Oct 30, 2012 at 23:07
  • 1
    Have you configured the remote database to accept remote connections from this user and host? Commented Oct 30, 2012 at 23:08

2 Answers 2

4
  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?

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

Sign up to request clarification or add additional context in comments.

2 Comments

It turns out the host provider is blocking remote connections to the database
sweet - glad you got it sorted :)
-3

I guess 314159265 could be replaced by some address.... like jdbc:mysql://localhost:3306/ or jdbc:mysql://127.0.0.1:3306/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.