0

I want to create a test to retrieve/update database entries.I am pretty new in automation testing and never used a connection to DB. For that I have a local connection with user and password and Northwind database installed. Everything is working fine in SQL Server Management Studio but the connection to database is not made in the below test I am trying to run:

import java.sql.*;


public class Driver {

    public static void main(String[] args) {
        try {
                Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:1433/Northwind", "coconut", "P@ssw0rd123");
            // 1.Get a connection to database
            // Create a statement
            Statement myStmt = myConn.createStatement();
            // Execute sql query
            ResultSet myRs = myStmt.executeQuery("select * from Employees");
            // Process result set
            while (myRs.next()){
                System.out.println(myRs.getString("LastName"));
            }
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }
}

after checking different tutorials I configured the corresponding jars in the Project Module : mysql-connector-java-8.0.21 sqljdbc4-2.0

In Sql Server Configuration Manager i fond out SQL is listening on port 1433, this is why i specified this in the connection url)

When running the above code i receive following error: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

T

he 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/java.sql.DriverManager.getConnection(DriverManager.java:677)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at Driver.main(Driver.java:8)
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:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    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)
    ... 6 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.base/sun.nio.ch.Net.connect0(Native Method)
    at java.base/sun.nio.ch.Net.connect(Net.java:503)
    at java.base/sun.nio.ch.Net.connect(Net.java:492)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
    at java.base/java.net.Socket.connect(Socket.java:648)
    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
    ... 9 more

Process finished with exit code 0

Any idea of what is wrong? Thank you!

jmx123
  • 1
  • Does this answer your question? [com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure](https://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai) – Saravana Kumar Aug 11 '20 at 14:02
  • Is your connection running I mean have you started your MySQL? – YUSOF KHAN Aug 11 '20 at 14:16
  • I checked in Management studio and the SQL agent is running. Is there another way to verify? – jmx123 Aug 11 '20 at 16:59

1 Answers1

0

It may be possible of below problems / issues ,

First Test your connection in your SQL Management Studio.It might not be started.

jdbc:mysql://localhost:1433/Northwind"

1.Make sure your port number, username and password is correct

2 Try IP address instead of writing localhost.

3.DB server has run out of connections.

Tamil.S
  • 403
  • 3
  • 14
  • thanks for answer. what do you mean that connection might not be started? I logged in the management studio with the corresponding username. This is what you mean? – jmx123 Aug 11 '20 at 17:05
  • From Database menu -> Manage connection -> Here Test your connection with username and password. – Tamil.S Aug 11 '20 at 17:30
  • Am assuming you are using MySQL Workbench – Tamil.S Aug 11 '20 at 17:30