日期:2014-05-17 浏览次数:20649 次
// 获取连接
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;
}