猿问

使用 Java 连接到 SQL 服务器时遇到问题

这是我在代码中编写的内容:


Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

String host1 = "<URL/Connection path string>";

String uname1= "<User name>";

String pwd1 = "<Password>";

Connection con1 =DriverManager.getConnection(host1,uname1,pwd1);

Statement stmt1 = con1.createStatement();

ResultSet rs = stmt1.executeQuery("<Query>");

我使用了以下 jar 文件:jtds-1.3.0、sqljdbc-4.1.5605、sqljdbc4-4.0


当我运行代码时,出现以下错误:


Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host <host-name>, named instance <instance-name> failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434.  For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.

    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)

    at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:3589)

    at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:1225)

    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:972)

    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)

    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)

    at java.sql.DriverManager.getConnection(DriverManager.java:571)

    at java.sql.DriverManager.getConnection(DriverManager.java:215)

    at first_doc.main(first_doc.java:28)

据我所知,没有阻止流量的防火墙。


另外,我是否需要添加端口号作为连接 URL 的一部分?


编辑:我使用 SQL 服务器版本 2008


我在配置管理器中找不到 SQLEXPRESS 的协议

互换的青春
浏览 399回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答