java 重用 PreparedStatement 实例的正确方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4958699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct way of reusing PreparedStatement instance?
提问by user291701
What's the right way to create a PreparedStatement, reuse it a few times, then clean it up? I'm using the following pattern:
创建 PreparedStatement、重用几次然后清理它的正确方法是什么?我正在使用以下模式:
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = getConnection(...);
// first use
stmt = conn.prepareStatement("some statement ?");
stmt.setString(1, "maybe some param");
if (stmt.execute()) {
...
}
// second use
stmt = conn.prepareStatement("some statement ?");
stmt.setString(1, "maybe some param");
if (stmt.execute()) {
...
}
// third use.
stmt = conn.prepareStatement("some statement");
stmt.execute();
}
finally {
if (stmt != null) {
try {
stmt.close();
} catch (Exception sqlex) {
sqlex.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (Exception sqlex) {
sqlex.printStackTrace();
}
conn = null;
}
}
Can we reuse the "stmt" object like that, or do we have to call stmt.close() between each query?
我们可以像这样重用“stmt”对象,还是必须在每个查询之间调用 stmt.close() ?
Thanks
谢谢
---------- Update ------------------------
- - - - - 更新 - - - - - - - - - - - -
Ah ok I see, each of my statements will be different. So is this a more correct pattern?:
啊好吧,我明白了,我的每一个陈述都会有所不同。那么这是一个更正确的模式吗?:
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = getConnection(...);
// first use
PreparedStatement stmt1 = null;
try {
stmt1 = conn.prepareStatement("some statement ?");
stmt1.setString(1, "maybe some param");
if (stmt1.execute()) {
...
}
}
finally {
if (stmt1 != null) {
try {
stmt1.close();
} catch (Exception ex) {}
}
}
// second use
PreparedStatement stmt2 = null;
try {
stmt2 = conn.prepareStatement("some different statement ?");
stmt2.setString(1, "maybe some param");
if (stmt2.execute()) {
...
}
}
finally {
if (stmt2 != null) {
try {
stmt2.close();
} catch (Exception ex) {}
}
}
// third use
PreparedStatement stmt3 = null;
try {
stmt3 = conn.prepareStatement("yet another statement ?");
stmt3.setString(1, "maybe some param");
if (stmt3.execute()) {
...
}
}
finally {
if (stmt3 != null) {
try {
stmt3.close();
} catch (Exception ex) {}
}
}
}
finally {
if (conn != null) {
try {
conn.close();
} catch (Exception sqlex) {
sqlex.printStackTrace();
}
conn = null;
}
}
So each different statement will be closed individually before the next one executes.
因此,每个不同的语句将在下一个执行之前单独关闭。
回答by jsegal
It's the other way around -- you only need to prepare it once, and then reuse it.
反之亦然——您只需要准备一次,然后重复使用。
I.E. This:
IE 这个:
// second use
stmt = conn.prepareStatement("some statement ?");
stmt.setString(1, "maybe some param");
if (stmt.execute()) {
...
}
should become this:
应该变成这样:
// second use
stmt.setString(1, "maybe some param");
if (stmt.execute()) {
...
}
Your third use, which is a different statement, should either be a new variable, or close your prepared statement first. (Though usually with PreparedStatements, you keep them around and reuse them).
您的第三次使用是一个不同的语句,应该是一个新变量,或者首先关闭准备好的语句。(尽管通常使用 PreparedStatements,您可以保留它们并重用它们)。