我尝试知道是否可以使用这些代码将 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/