在 JDBC 中,我可以使用单个Statement
调用对象executeQuery("")
多次?安全吗?或者我应该在每个查询后关闭语句对象,并创建新对象来执行另一个查询。
E.G:
Connection con;
Statement s;
ResultSet rs;
ResultSet rs2;
try
{
con = getConnection();
// Initially I was creating the Statement object in an
// incorrect way. It was just intended to be a pseudocode.
// But too many answerers misinterpretted it wrongly. Sorry
// for that. I corrected the question now. Following is the
// wrong way, commented out now.
// s = con.prepareStatement();
// Following is the way which is correct and fits for my question.
s = con.createStatement();
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);
}
是的,您可以重复使用Statement
(具体来说是一个PreparedStatement
)并且通常应该使用 JDBC 来执行此操作。如果您不重复使用您的语句并立即创建另一个相同的语句,那么这将是低效且糟糕的风格Statement
目的。至于关闭它,最好在finally块中关闭它,就像您在这个片段中一样。
有关您所要求的示例,请查看此链接:jOOq 文档 http://www.jooq.org/doc/3.2/manual/sql-execution/reusing-statements/#N129B1
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)