Connecting to MS SQL DB from Java thows error

56 Views Asked by At

I have MS SQL running on my Mac, installed and configured over Docker - As instructed in - https://www.devart.com/dbforge/sql/studio/install-sql-server-on-mac.html.

Below is my code, which I tried to connect to the Database. The server is up and running.

But I get the mentioned error, and I am not sure what is the exact problem:

import java.sql.*;
import javax.swing.*;

public class DBConnectTrial1 {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
      String driver = "com.mysql.jdbc.Driver";
      String url =
          "jdbc:mysql://localhost:3306/mssql_trial?enabledTLSProtocols=TLSv1.2";
      String username = "sa";
      String password = "DB_Password";

      Class.forName(driver);

      Connection conn = DriverManager.getConnection(url, username, password);
      // return conn;

      // JOptionPane.showMessageDialog(null, "Connected to the database");

      System.out.println("Connection established");
    }

    catch (Exception e) {
      // JOptionPane.showMessageDialog(null, "Could not connect to the
      // database...");

      // return null;
      e.printStackTrace();
    }
  }
}
Exception :

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 java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:357)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2479)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:683)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:230)
    at DBConnectTrial1.main(DBConnectTrial1.java:19)
Caused by: java.net.ConnectException: Connection refused
    at java.base/sun.nio.ch.Net.connect0(Native Method)
    at java.base/sun.nio.ch.Net.connect(Net.java:589)
    at java.base/sun.nio.ch.Net.connect(Net.java:578)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:583)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:751)
    at java.base/java.net.Socket.connect(Socket.java:686)
    at java.base/java.net.Socket.<init>(Socket.java:555)
    at java.base/java.net.Socket.<init>(Socket.java:356)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:307)
    ... 14 more

Can someone please help me to find a solution. I imported the jar file - mysql-connector-java-5.1.25.jar and configured inside the project. I am using eclipse IDE.

2

There are 2 best solutions below

2
AndrewL On BEST ANSWER

Which database brand are you connecting to? You seem to talk about running Microsoft SQL Server, but you are using connection settings for MySQL (now owned by Oracle).

  • MySQL's
    • default port is 3306
    • driver is typically com.mysql.jdbc.Driver
  • Microsoft SQLServer
0
user434853 On

Thanks to all who helped me to fix this. Here is the code.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DBConnectTrial1 {

public static void main(String[] args) {


    String  connectionURL = "jdbc:sqlserver://localhost:1433;databaseName=mssql_trial;user=sa;password=DB_Password;encrypt=true;trustServerCertificate=true";
    try {
        System.out.print("Connecting to the server......");
        try (Connection connection = DriverManager.getConnection(connectionURL))   {
            System.out.println("Connected to the Server.");
        }
    }catch (Exception e){
        System.out.println("I am not connected to the Server");
        e.printStackTrace();
    }
}
}