代码优化遇到的小问题:sqlStatement与ResultSet忠诚爱情

最近在优化一个老项目的代码,处理数据库连接部分发现这样几行语句。

 public ResultSet openRs(String sql) throws SQLException
  {
    Statement sqlStatement1 = this.dbCon.createStatement(1005, 1008);
    ResultSet rs = sqlStatement1.executeQuery(sql);
    return rs;
  }


 

当时想每次执行sql都要创建sqlstatement多浪费啊,于是就改掉了,变成全局对象,结果报错了。

后来认真查阅了下API,原来sqlstatement与ResultSet是终生一对一的爱情,不是一对多的滥情,好专一啊。

 

 * By default, only one <code>ResultSet</code> object per <code>Statement</code>
 * object can be open at the same time. Therefore, if the reading of one
 * <code>ResultSet</code> object is interleaved
 * with the reading of another, each must have been generated by
 * different <code>Statement</code> objects. All execution methods in the
 * <code>Statement</code> interface implicitly close a statment's current
 * <code>ResultSet</code> object if an open one exists.
 *

其实问题很简单,就是有点让我意想不到,想不到自己想当然的竟然是错的,看来已经好久没有注意代码细节了,更让我想不明白的是为什么要建立这种对应关系,其优点在哪里。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值