我找到了一个用于jdbc操作的实用程序类。
public ResultSet executePreparedStatementQuery(String sql, ArrayList args)
throws ValidationException {
try {
InitialContext ctx = new InitialContext();
DataSource ds = getDataSource(ctx);
conn = ds.getConnection();
CustomLogger.getLogger("JDBCUtility").logConnection(conn, true, 3);
pStmt = conn.prepareStatement(sql);
int count = 1;
Iterator iter = args.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
setPreparedStatementField(count, obj);
count++;
}
rs = pStmt.executeQuery();
} catch (SQLException sqlEx) {
close();
throw new ValidationException(sqlEx.getMessage());
} catch (Exception cnfe) {
close();
cnfe.printStackTrace();
throw new ValidationException(cnfe.getMessage());
}
return rs;
}
所有的连接,语句和结果集都是由非静态类变量引用的。这会在anather类中创建一些JDBCUtility实例(dbutil)后调用rs = dbutil.executePreparedStatementQuery(strQuery, values);
时泄漏内存吗?
提前谢谢..!