Heim > Datenbank > MySQL-Tutorial > Hauptteil

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

WBOY
Freigeben: 2016-06-07 15:18:18
Original
1292 Leute haben es durchsucht

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

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

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!