Home  >  Article  >  Database  >  关于jdbc链接SQLServer数据库的一些疑问

关于jdbc链接SQLServer数据库的一些疑问

WBOY
WBOYOriginal
2016-06-07 15:18:181283browse

源代码是: 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

请各位高手帮忙看一下是咋回事呢,连了一晚上加一早上了,还是连不上,唉……

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn