简体   繁体   中英

Error while connecting to Microsoft SQL Server

I am trying to connect to MS SQL Server 2005 from java, I am getting the following error, can anyone help me ?

private static final String DRIVER_CLASS = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; 
Class.forName(DRIVER_CLASS); 
DriverManager.getConnection("jdbc:sqlserver://localhost:1433","sa","root");

I added the following 5 jar files :

  • msbase
  • mssqlserver
  • msutil
  • sqljdbc
  • sqljdbc4

The exception is :

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
 at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
 at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
 at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
 at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
 at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
 at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
 at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
 at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
 at java.sql.DriverManager.getConnection(Unknown Source)
 at java.sql.DriverManager.getConnection(Unknown Source)

Is you firewall on? If so, maybe you will need to make an exception for port 1433

Please check what protocols are enabled on the server. Go to Start/Programs/Microsoft |SQL Server 2005/Configuration Tools/SQL Server Configuration Manager. Check if tcp is enabled under SQL Server 2005 Network Configuration/Protocols for MSSQLSERVER.

Please check if you don't have named instance - SQL Server Express edition by default creates a named instance for example.

And the third - check if SQL Server has mixed mode authentication - if not it will not allow you to connect using SQL login and password. BTW it is a bad idea to use sa account to develop applications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM