0

I've been having trouble trying to connect my MySQL database to java using JDBC. I have been following this set of tutorials https://www.youtube.com/watch?v=azsXYnkhBuk&list=PLhs1urmduZ2-yp3zID5rMEmXDETN8xvMo&index=3 I've been using apache Netbeans IDE 11.3 and here is my source code. A screenshot of what my package organization looks like is attached. So now to the error. I was able to get it to say "database connection success" before I added the query so It was showing some connection I think?

package databaseproject;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;   
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;


public class DatabaseProject {


    public static void main(String[] args) {
      DatabaseProject pro = new DatabaseProject();
            pro.createConnection();
     }

    void createConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vesseldata", "root", "root");
            Statement stat = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM VESSELS");
            while(rs.next()){
                String name = rs.getString("name");
                System.out.println(name);
            }
            System.out.println("Database Connection Success");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DatabaseProject.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(DatabaseProject.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

Here is an image of how my package is organized

Heres what the error looks like

Loading class 'com.mysql.jdbc.Driver'. This is deprecated. The new driver class is 'com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
May 11, 2020 11:34:26 AM databaseproject.DatabaseProject createConnection
SEVERE: null
com.mysql.cj.jdbc.exceptions.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 com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at databaseproject.DatabaseProject.createConnection(DatabaseProject.java:25)
    at databaseproject.DatabaseProject.main(DatabaseProject.java:18)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
    at com.mysql.cj.NativeSession.connect(NativeSession.java:144)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
    ... 7 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
    ... 10 more

BUILD SUCCESSFUL (total time: 10 seconds)

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • First lines of error `Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.` So try to remove `Class.forName("com.mysql.jdbc.Driver");` since it looks like your MySQL version doesn't use `com.mysql.jdbc.Driver` any longer but is expecting `iscom.mysql.cj.jdbc.Driver` which (based on last part of that message) should be loaded automatically. – Pshemo May 11 '20 at 15:44
  • Remove the deprecated and latest one.. – Kumar Anil Chaurasiya May 11 '20 at 15:50
  • 1
    @Pshemo The first one is a **warning** not an error, because the OP is loading a deprecated class instead of the new recommend class (although explicitly loading the driver isn't even necessary). The actual problem is the "com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure". – Mark Rotteveel May 11 '20 at 16:07
  • @MarkRotteveel Thanks for clarification. Now that I think about it, it simply makes sense to not generate error while loading `com.mysql.jdbc.Driver` but warning since proper Driver will be loaded automatically regardless. – Pshemo May 11 '20 at 16:17
  • @MarkRotteveel BTW I tried to format the error message in the question to be more readable, but noticed that class names there are surrounded using not matching quotes like `\`name.of.SomeClass'` which prevents formatting to use correct color. To correct coloring I changed first `\`` into `'`. Do you think it is acceptable (would search engines be able to find `Loading class '...'` even while searching for `Loading class \`..'`? Or maybe there is some better way here? – Pshemo May 11 '20 at 16:22
  • @Pshemo The presence of a backtick in a code block shouldn't change the highlight, so that change wouldn't be necessary, but it looks OK as it is now. – Mark Rotteveel May 11 '20 at 16:24

0 Answers0