Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

sql server - Can't connect to database using JDBC (Communications link failure) despite being able to connect by other means

I am running MSSQL on docker using port 1433:

enter image description here

And am able to connect to it and make queries using a simple tool I downloaded from the internet (SQLDbx):

enter image description here

However when I try to connect using JDBC by this code:

  public static void main(String[] args) {

    try {
        
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:1433/SocialDB/", "myUsername","myPW");
        
        con.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

I get:

    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.

I wonder if there is some extra step that I have to do when running SQLServer on docker that I am missing, and if not, I wonder what could be causing the problem. Thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the port number is the default one:

String connectionUrl = "jdbc:sqlserver://localhost;database=SocialDB;integratedSecurity=true;"  

localhost:1433 is the same as localhost


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...