时间:2021-07-01 10:21:17 帮助过:21人阅读
源代码是: import java.sql.*; public class ConnectionDemo { static String driver = com.microsoft.sqlserver.jdbc.SQLServerDriver; private static final String url = jdbc:sqlserver://localhost:1433;DatabaseName=MyDB; public static void main(S
源代码是:
import java.sql.*;
public class ConnectionDemo {
static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String url = "jdbc:sqlserver://localhost:1433;DatabaseName=MyDB";
public static void main(String[] args) {
Connection con = null;
try {
Class.forName(driver); // 加载驱动
con = DriverManager.getConnection(url, "sa", "sa");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(con);
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行之后报错:
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: Connection refused: connect. Please verify the connection properties and check that a SQL Server instance is running on the host and
accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1195)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(SQLServerConnection.java:1054)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:758)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at ch6.practise.ConnectionDemo.main(ConnectionDemo.java:14)
java.lang.NullPointerException
at ch6.practise.ConnectionDemo.main(ConnectionDemo.java:22)
null
请各位高手帮忙看一下是咋回事呢,连了一晚上加一早上了,还是连不上,唉……