gpt4 book ai didi

java - TCP/IP(Java 应用程序到 SQL Server)

转载 作者:太空宇宙 更新时间:2023-11-04 07:43:00 24 4
gpt4 key购买 nike

我尝试知道是否可以使用这些代码将 Java 连接到 SQL Server:

package pkgtry;
import java.sql.*;
public class NewMain {

public static void main(String[] args) {
String connectionUrl="jdbc:sqlserver://(local):1433;DatabaseName=OJT;user=sa;password=''";


Connection con = null;
Statement stmt = null;
ResultSet rs = null;

try {

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);


String SQL = "";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);


while (rs.next()) {

}
}


catch (Exception e) {
e.printStackTrace();
}

finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
}

但在我这边它显示一个错误并显示:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host (local), port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".

这是什么意思?如何解决?

最佳答案

这意味着 JDBC 无法连接到数据库服务器。它找不到主机(local),您的意思是在这里放置localhost而不是(local)吗?

String  connectionUrl="jdbc:sqlserver://(local):1433;DatabaseName=OJT;user=sa;password=''";

关于java - TCP/IP(Java 应用程序到 SQL Server),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15756734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com