我正在使用通过sqljdbc4.jar
驱动程序连接到SQL Server 2012的NetBeans 8.2。
我创建一个名为Provider的接口Java类:
public interface Provider {
String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://DESKTOP-MF8EEF8;DatabaseName=db_Class_Payment;integrated Security=false;";
String username="sa;";
String password="123;";
}
然后将Provider调用到名为DBFactory的连接类中。
public class DBFactory {
static Connection con;
static
{
try
{
Class.forName(driver);
con = DriverManager.getConnection(url,username,password);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static Connection getcon()
{
return con;
}
}
然后我用下面的代码进行测试:
if(con==null){
JOptionPane.showMessageDialog(null, "Connecting failed");
}else{
JOptionPane.showMessageDialog(null, "Connecting successfully.");
}
该过程使我连接失败或连接null并出现错误msg;
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host DESKTOP-MF8EEF8, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at Class.DBFactory.<clinit>(DBFactory.java:18)
at Frame.Home.<init>(Home.java:16)
at Frame.Home$4.run(Home.java:449)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 23 seconds)
我已经将sqljdbc4.jar库添加到项目中的库中。
它仍然给出错误结果。
答案 0 :(得分:0)
您的连接url
错误。
integrated Security=false;
应该是
integratedSecurity=false;
阅读Building the Connection URL会变得更好。