问题描述
我正在使用端口 1433 在 docker 上运行 MSsql:
并且能够连接到它并使用我从互联网下载的简单工具 (sqlDbx) 进行查询:
但是,当我尝试通过此代码使用 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 相同