JDBC查询SQLServer2008速度很慢
用了sqljdbc4.jar这个包去访问SQLServer2008,仅仅是做查询,而且是按索引查的,用企业管理器查瞬间(毫秒级)就出来了,用JDBC查要秒级,代码如下:
// 获取连接
public void getConnect()
{
try {
//注册驱动程序
Class.forName(driverNameOfSqlServer);
Properties info = new Properties();
info.put("user", databaseUser);
info.put("password", databasePassword);
info.put("sendStringParametersAsUnicode", "false");
// 获取连接
connect = DriverManager.getConnection(databaseUrl, info);
connect.setReadOnly(true);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
// 获取结果集
public ResultSet getResultSetQuery(String sql)
{
try
{
long startTime = new Date().getTime();
PreparedStatement statement = connect.prepareStatement(sql,
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
ResultSet resultSet = statement.executeQuery();
CachedRowSet rowSet = new CachedRowSetImpl();
rowSet.populate(resultSet);
resultSet.close();
statement.close();
long spanTime = new Date().getTime() - startTime;
if(spanTime > 1000)
logger.info("Too slow when query db:" + spanTime);
return rowSet;
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return null;
}