使用ResultSet更新数据库

 

package book.database;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UpdateWithResultSet {

 public static void update(Connection con){
  String sql="select * from student_basic";
  Statement sm=null;
  ResultSet rs=null;
  try{
   sm=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
   rs=sm.executeQuery(sql);
   rs.absolute(1);
   rs.updateDouble("score",70);
   rs.updateString(5,"updated by ResultSet!");
   
   rs.updateRow();
   OperateDB.showResultSet(rs);
   
   System.out.println("准备用ResultSet插入一条记录!");
   rs.moveToInsertRow();
   rs.updateInt("ID",7);
   rs.updateString("name","mike");
   rs.updateInt("age",26);
   rs.updateInt("score",88);
   rs.updateString("description","a super man");
   rs.insertRow();
   OperateDB.showResultSet(rs);
   
   rs.last();
   System.out.println("准备用ResultSet删除最后一条记录!");
   rs.deleteRow();
   OperateDB.showResultSet(rs);
   rs.close();
  }catch(SQLException e){
   e.printStackTrace();
  }finally{
   OperateDB.closeStatement(sm);
  }
 }
 public static void main(String[] args) throws ClassNotFoundException,SQLException{
  String dbName="SGCC";
  String userName="wuhailin";
  String password="wuhailin";
  Connection con=null;
  try{
   con=DBConnector.getOracleConnection(null,null,null, dbName, userName, password);
   UpdateWithResultSet.update(con);
  }catch(ClassNotFoundException e1){
   throw e1;
  }catch(SQLException e2){
   throw e2;
  }finally{
   OperateDB.closeConnection(con);
  }

 }

}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页