Hello,

I am trying to connect to a hosted database on netbeans(java).

So far I have the following:

Connection con = null;
        Properties conProps = new Properties();
        conProps.setProperty("user", "user");
        conProps.setProperty("password", "password");

        try {
            con = DriverManager.getConnection("jdbc:mysql://host:3306/dbname", conProps);
            String sql = ("SELECT * from products");
            st = (com.mysql.jdbc.PreparedStatement) con.prepareStatement(sql);
            st.executeQuery();

            rs = st.getResultSet();

So my host looks something like this: XX.XXX.X.XX which represents numbers.

I am definelty doing something wrong as it is not connected.

I am sure that the host is correct as well as username, password and db name.

Error is :

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.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:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    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:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:187)
    at Table.populateList2(Table.java:57)
    at Table.<init>(Table.java:34)
    at Table$3.run(Table.java:165)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)

It does work when connected to local host but not a hosted database

Nope, My host looks something like this: XX.XXX.X.XX which represents numbers.

Are you hosting it yourself? Check ports and whether the host allows remote access to a MySQL database. Also check you have the proper user privileges in place (GRANT).

Also you might be having trouble because of the database that you're using. Such as MySQL, or MongoDB.

If you are trying to connect to a real server..you need to configure the server allowing remote database connections..like adding your IP address on the remote connection list

Edited 1 Year Ago by cool_zephyr

Thanks guys, I added my ip address at home so that it allows it. It worked yesterday but now I am at university and it does not work..

I tried to add the other IP address, no success. I will try again at home and let you know

@James, I did not write "host" in my code. For security reasons, I did not want to add it here.

I did write the correct host address.

Edited 1 Year Ago by london-G

This question has already been answered. Start a new discussion instead.