在将其标记为重复之前请先阅读.我从谷歌和SO搜索了很多,但我无法确切的回答我正在寻找.
我的问题:在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);
}