0

I realize many have already posted about this issue, but I've been trying whatn I find and had issues... Just trying to set up a basic connection to the mySQL server using JDBC in eclipse for a class project. Thus far I'm the only one having issues connecting even though I'm following the same steps and even using the same file. The code itself is below (minus the UN/PW followed by the errors I'm getting through the printStackTrace:

        try{
        Class.forName("com.mysql.cj.jdbc.Driver");
    Connection connectionValue = DriverManager.getConnection(sqldatabase, username, passwordValue);
    System.out.println("sucess");
    Statement sqlStatement = connectionValue.createStatement();
    boolean value = sqlStatement.execute("Select * from Inventory_Items");
    // prints out a boolean value, so if you check if a user exists you can do a quick comparison ** login function
    System.out.println(value);
    // prepared statements knowledge is from https://www.javatpoint.com/PreparedStatement-interface
    // ************************* UPDATE**************************
    String updateButtonSQL = "UPDATE Inventory_Items SET itemvalue=? WHERE item=?"; 
    PreparedStatement updateValue = connectionValue.prepareStatement(updateButtonSQL);
    updateValue.setString(2, "apples"); // this is related to which question mark, this is for the second question mark
    updateValue.setInt(1, 4); // this is related to the FIRST questionmark
    int test = updateValue.executeUpdate();// this executes the statement
    System.out.println(test); // 1 means success
    // ******************************** ADD ***********************************
    String addButtonSQL = "INSERT INTO Inventory_Items (item, itemValue) VALUES (?,?)";
    PreparedStatement addItemValue = connectionValue.prepareStatement(addButtonSQL);
    addItemValue.setString(1, "Pens");
    addItemValue.setInt(2, 7);
    int addTest = addItemValue.executeUpdate();

    // ************************** DELETE *********************************
    String deleteButtonSQL = "DELETE FROM Inventory_Items WHERE item=?";
    PreparedStatement deleteItemValue = connectionValue.prepareStatement(deleteButtonSQL);
    deleteItemValue.setString(1, "Pens");
    int deleteTest = deleteItemValue.executeUpdate();
    // ************************ SELECT ***************************
    // code was basically copied from https://www.javatpoint.com/PreparedStatement-interface
    String selectButtonSQL = "Select * from Inventory_Items";
    PreparedStatement selectItemValue = connectionValue.prepareStatement(selectButtonSQL);
    ResultSet selectResponse = selectItemValue.executeQuery();
    
    while(selectResponse.next()){
        System.out.println(selectResponse.getString(1)+ " "+ selectResponse.getInt(2));
    }
    
    
    }
    catch(SQLException io){
        System.out.println("it failed to connect");
    }
    catch(ClassNotFoundException ii){
        System.out.println("class failed");
    }

And the errors:

    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 mysql.connector.java@8.0.29/com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
    at mysql.connector.java@8.0.29/com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
    at mysql.connector.java@8.0.29/com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828)
    at mysql.connector.java@8.0.29/com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)
    at mysql.connector.java@8.0.29/com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
    at mysql.connector.java@8.0.29/com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
    at test.JavaApplication1.main(JavaApplication1.java:25)
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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at mysql.connector.java@8.0.29/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at mysql.connector.java@8.0.29/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    at mysql.connector.java@8.0.29/com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
    at mysql.connector.java@8.0.29/com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
    at mysql.connector.java@8.0.29/com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89)
    at mysql.connector.java@8.0.29/com.mysql.cj.NativeSession.connect(NativeSession.java:120)
    at mysql.connector.java@8.0.29/com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:948)
    at mysql.connector.java@8.0.29/com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818)
    ... 6 more
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.base/sun.nio.ch.Net.connect0(Native Method)
    at java.base/sun.nio.ch.Net.connect(Net.java:579)
    at java.base/sun.nio.ch.Net.connect(Net.java:568)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:633)
    at mysql.connector.java@8.0.29/com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153)
    at mysql.connector.java@8.0.29/com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
    ... 9 more

I'm sure it something people here could fix in 5 minutes, but I'm a student and i'm lost....

MS_Warrior
  • 43
  • 8
  • 1
    The same code works on other machines and not on yours. We'd need to _have your machine_ to debug this, obviously. The error isn't lying: The network connection fails (the 'connect' timed out, meaning, it's like the server you're connecting to doesn't exist or isn't turned on. Maybe you're behind a firewall, maybe the SQL server really is off. – rzwitserloot Jul 05 '22 at 12:57
  • That's a network issue. Are you sure the cable is plugged in? Is the server up & running? Is your wifi good? – The Impaler Jul 05 '22 at 12:57
  • Where did you see these errors? It's impossible that your code will output the stack trace as shown, as you are swallowing the exceptions in the two `catch` blocks. – tquadrat Jul 05 '22 at 13:02
  • I've tried hardwired and wifi, The server is up and running. I've tried with and without the firewall turned off – MS_Warrior Jul 05 '22 at 13:03

0 Answers0