可更新和对更新敏感的结果集

可更新的结果集(说明这些都是JDBC特殊的用法,有些数据库可能不支持)

       conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,

                                   ResultSet.CONCUR_UPDATABLE);

       rs.updateString("col name", "new value");

       rs.updateRow();

      

public class OtherApi {

    public static void main(String[] args) throws SQLException,

           InterruptedException {

           read();

    }

 

    static void read() throws SQLException, InterruptedException {

       Connection conn = null;

       Statement st = null;

       ResultSet rs = null;

       try {

           // 2.建立连接

           conn = JdbcUtils.getConnection();

           // conn = JdbcUtilsSing.getInstance().getConnection();

           // 3.创建语句

           st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,

                  ResultSet.CONCUR_UPDATABLE);

           //ResultSet.TYPE_SCROLL_INSENSITIVEResultSet.TYPE_SCROLL_SENSITIVE区别

           //TYPE_SCROLL_SENSITIVE能够感应到数据库中的变化,即在未出查询得到结果集ResultSet时如果数据库在的

           //记录发生改变了,查询得到结果集ResultSet它能够知道.当然这样会消耗性能的.

           //TYPE_SCROLL_INSENSITIVE是不能够感应到数据库中的变化.

           // 4.执行语句

           rs = st

                  .executeQuery("select id, name, money, birthday  from user where id < 5");

 

           // 5.处理结果

           while (rs.next()) {

              int id = rs.getInt("id");

              System.out.println("show " + id + "...");

              System.out.println(id + "\t" + rs.getObject("name") + "\t"

                     + rs.getObject("birthday") + "\t"

                     + rs.getObject("money"));

              //在结果集中修改结果集,数据库也修改了。如下面把李四money修改为500f了。

              String name=rs.getString("name");

              if(name.equals("lisi")){

                  rs.updateFloat("money", 500f);

                  rs.updateRow();

              }

           }

       } finally {

           JdbcUtils.free(rs, st, conn);

       }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值