在使用jdbc连接SqlServer2008时出现如上错误,很有可能是建立连接之后,在使用完没有关闭。造成异常。i
注意如下红色字体部分
public Connection getConnection() throws SQLException, NamingException
{
Connection con = null;
try
{
String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String URL = "jdbc:microsoft:sqlserver://localhost:1434;DatabaseName=assetManager";
String Username = "test";
String Password = "test123456";
Class.forName(Driver);
con = DriverManager.getConnection(URL, Username, Password);
}
catch (Exception e)
{
System.out.println("This is error message from getConnection() ==" + e);
}
return con;
}
protected int getDepartment_idByName(String name) throws SQLException, NamingException
{
int department_id=1;
Connection conn = getConnection();
String s = "select department_id from department where department_name = ?";
PreparedStatement stmt = conn.prepareStatement(s);
stmt.setString(1,name);
ResultSet rs = stmt.executeQuery();
if (rs.next())
{
department_id = rs.getInt("department_id");
}
rs.close();
stmt.close();
conn.close();
return department_id;
}
在解决该问题之前还出现了另一个错误
SQL Server 2000 "Error establishing socket"
在网上搜索之后发现很有可能是端口号的错误,正常默认情况下是1433,但最好还是查询一下
新建查询 输入exec sys.sp_readerrorlog 0, 1, 'listening'
即可查看到端口号截图如下
String URL = "jdbc:microsoft:sqlserver://localhost:1434;DatabaseName=assetManager";
至此问题解决,废了这么久的时间解决这样的一个小问题,必须记录下来备忘