Java语句对象重用? [英] Java Statement Object Reuse?

查看:72
本文介绍了Java语句对象重用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如,

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 + "')");  

在上述情况下,对两个executeUpdate()查询使用相同的语句st1是否有任何危害?我可以将相同的Statement对象st1用于另一个executeQuery()吗?

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()?

推荐答案

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

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

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

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.

这篇关于Java语句对象重用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆