我正在尝试使用 SQLJDBC4 jar 文件和 JDK 1.6 与 SQL Server 2008 R2 建立 jdbc 连接。我正在使用 Netbeans IDE 并添加了 SQLJDBC4 jar 并在服务的“数据库”部分中添加了数据库的路径。代码如下:
package connect2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Connect2 {
public static void main(String[] args) throws SQLException {
Connection conn;
conn = null;
System.out.println("Done....");
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection ("jdbc:sqlserver://172.17.39.13\\CRM:1433;databaseName=crm_xchanging","crm_xchanging","Welcome001");
System.out.println ("Database connection established");
}
catch (ClassNotFoundException e)
{
System.out.println (e);
}
catch (SQLException ex)
{
System.out.println(" error");
}
finally
{
if (conn != null)
{
try{
Statement st = conn.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM usertable");
System.out.println("User Name: " );
while (res.next()) {
String employeeName = res.getString("user_name");
System.out.println(employeeName);
}
conn.close();
}
catch(SQLException ex){
System.err.println("SQLException information");
while(ex!=null) {
System.err.println ("Error msg: " + ex.getMessage());
System.err.println ("SQLSTATE: " + ex.getSQLState());
System.err.println ("Error code: " + ex.getErrorCode());
ex = ex.getNextException();
// For drivers that support chained exceptions
}}
}
}
}
}
这是我得到的输出:
run:
Done....
Database connection established
SQLException information
Error msg: Connection reset
SQLSTATE: 08S01
Error code: 0
BUILD SUCCESSFUL (total time: 1 second)
我认为代码或JDK没有任何错误。我也尝试设置最大编号。SQL Server 的活动连接数为 0(无限)。我该如何解决这个问题?