使用单个 JDBC 语句对象执行多个查询
在 JDBC 中,是否可以使用单个对象多次调用?它安全吗?或者,我应该在每个查询后关闭语句对象,并创建用于执行另一个查询的新对象。Statement
executeQuery("")
例如:
Connection con;
Statement s;
ResultSet rs;
ResultSet rs2;
try
{
con = getConnection();
s = con.prepareStatement();
try
{
rs = s.executeQuery(".......................");
// process the result set rs
}
finally
{
close(rs);
}
// I know what to do to rs here
// But I am asking, should I close the Statement s here? Or can I use it again for the next query?
try
{
rs2 = s.executeQuery(".......................");
// process the result set rs2
}
finally
{
close(rs2);
}
}
finally
{
close(s);
close(con);
}