java Java语句对象重用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/389832/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:11:44  来源:igfitidea点击:

Java Statement Object Reuse?

javajdbc

提问by Epitaph

I would like to know if we can reuse the same Statement object for executing more than one query. Or, should we create a new statement for different queries.

我想知道我们是否可以重用同一个 Statement 对象来执行多个查询。或者,我们应该为不同的查询创建一个新语句。

For example,

例如,

Connection con = getDBConnection();
Statement st1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
int i = st1.executeUpdate("update tbl_domu set domU_status=1 where domU_id=" + dom_U_id);
Statement st2 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
int j = st2.executeUpdate("insert into tbl_domU_action_history values('" + dom_U_name + "', 1, '" + date + "')");  

In the above case, is there any harm in using the same statement st1 for both the executeUpdate() queries? Can I use the same Statement object st1 for another executeQuery()?

在上述情况下,对两个 executeUpdate() 查询使用相同的语句 st1 有什么害处吗?我可以对另一个 executeQuery() 使用相同的 Statement 对象 st1 吗?

回答by Epitaph

I came across the response I was looking for in the Javadocs

我在 Javadocs 中找到了我正在寻找的响应

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects.

默认情况下,每个 Statement 对象只能同时打开一个 ResultSet 对象。因此,如果一个 ResultSet 对象的读取与另一个的读取交错,则每个对象都必须由不同的 Statement 对象生成。

回答by Tom Hawtin - tackline

Yes, you can. However, it is very much better to use PreparedStatementto avoid SQL injection vulnerabilities.

是的你可以。但是,使用它PreparedStatement来避免SQL注入漏洞要好得多。

回答by Steve B.

The original point of using prepared statements was to avoid having the database parse and recompile the statement, so it's supposed to be faster.

使用准备好的语句的最初目的是避免让数据库解析和重新编译语句,因此它应该更快。

I had not considered the SQL injection vulnerabilities use, but I'm not sure what, if any data checking is done. I suspect that it's driver-dependant, as the driver implementation is free to just glue the statements together. If anyone has further details, please post.

我没有考虑过 SQL 注入漏洞的使用,但我不确定是什么,如果进行了任何数据检查。我怀疑它依赖于驱动程序,因为驱动程序实现可以自由地将语句粘合在一起。如果有人有更详细的信息,请留言。

回答by Ted Johnson

I am not sure that the result sets get garbage collected if only they are only closed. I think I had a cool memory leak when I was writing a connection pool so the same statement/connection was re-used a million times. Kept bringing down my JVM. I guess this could also be implementation specific as garabage collection of a closed result set may not be tested.

我不确定结果集是否会在仅关闭时被垃圾收集。我认为我在编写连接池时遇到了很酷的内存泄漏,因此相同的语句/连接被重用了一百万次。不断降低我的 JVM。我想这也可能是特定于实现的,因为可能无法测试封闭结果集的垃圾收集。

回答by Powerlord

Whenever you assign something to a reference type, you're replacing the old reference with a new one.

每当您将某些内容分配给引用类型时,您就是在用新引用替换旧引用。

For example...

例如...

MyObject obj = new MyObject("foo");
obj = new MyObject("bar");

Would have a now non-referenced instance of MyObject with some property set to "foo" that will eventually be garbage collected.

将有一个现在未引用的 MyObject 实例,其某些属性设置为“foo”,最终将被垃圾收集。

obj stores a reference to a MyObject with some property set to "bar".

obj 存储对 MyObject 的引用,其中某些属性设置为“bar”。