尽管能够通过其他方式连接,但无法使用 JDBC 连接到数据库通信链接失败

问题描述

我正在使用端口 1433 在 docker 上运行 MSsql

enter image description here

并且能够连接到它并使用我从互联网下载的简单工具 (sqlDbx) 进行查询

enter image description here

但是,当我尝试通过此代码使用 JDBC 进行连接时:

  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);
    }
}

我明白了:

    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.

我想知道在 docker 上运行 sqlServer 时是否需要执行一些我遗漏的额外步骤,如果没有,我想知道是什么导致了问题。 提前致谢。

解决方法

如果端口号是默认的:

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

localhost:1433 与 localhost 相同

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...