【发布时间】:2014-09-08 21:16:02
【问题描述】:
我在连接到我在我的电脑中为一个小型应用程序创建的 SQL Server 数据库时遇到问题。服务器名为“INSPRONN5110-D”。我使用 Windows 身份验证登录到服务器。已从 microsoft 下载驱动程序并将其内容复制到程序文件文件夹并将路径(C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc4.jar)添加到路径和类路径变量(不确定使用哪一个)并且仍然出现此错误。
Creating Connection.
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host INSPIRONN5110-D, port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
.....
这是我尝试连接到数据库的方法。
private Connection connectToDB() {
String connectionUrl = "jdbc:sqlserver://INSPIRONN5110-D; databaseName=MemoryHelp;";
Connection con = null;
try{
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("Creating Connection.");
con = DriverManager.getConnection(connectionUrl);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// } catch (ClassNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
} finally{
System.out.println("Connection created.");
}
return con;
}
尝试将连接 url 更改为“jdbc:sqlserver://localhost; databaseName=MemoryHelp;”
现在收到此错误:
Creating Connection.
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
....
终于解决了。必须转到 SQL Server 配置管理器并启用与服务器的 tcp/ip 连接,然后将 sqljdbc_auth.dll 文件从驱动程序文件夹复制到 system32 文件夹并使用以下连接 URL 可以使其工作:
String connectionUrl = "jdbc:sqlserver://localhost;databaseName=MemoryHelp;integratedSecurity=true";
【问题讨论】:
-
要确定这是 JDBC 问题还是一般网络问题,请尝试使用完全相同的主机名、端口、用户名和密码通过 SQL Server Management Studio 连接到服务器。如果失败,那么您的问题与 JDBC 无关。
-
如上所述,我使用 Windows 帐户通过 SQL Server Management Studio 连接到服务器(如果有帮助,我使用的是 Windows 8.1)。我想在这里也一样。我在 Management Studio 中没有任何问题。
标签: java sql-server jdbc