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.
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).
3306
com.mysql.jdbc.Driver
3306
see https://stackoverflow.com/a/49669700/1847378com.microsoft.sqlserver.jdbc.SQLServerDriver