PreparedStatement 批量更新,插入数据到Oracle
2016-06-16 09:59
507 查看
/** * 更新数据库已有的customer信息 * @param List<CustomerBean> * @return */ public int updateExistsInfo(List<CustomerBean> updateList){ //查询的SQL语句 String sql = "update t_customer set LICENSE_KEY=?,CORPORATE_NAME=?,INTEGRATED_CLASSIFICATION=?,BOSSHEAD=?," + "CONTACT_PHONE=?,ORDER_FREQUENCY=?,CONTACT_ADDRESS=?,USER_ID=? where CUSTOMER_ID=?" ; //插入需要的数据库对象 Connection conn = null; PreparedStatement pstmt = null; try { conn = new DBSource().getConnection(); //设置事务属性 conn.setAutoCommit(false); pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); for(CustomerBean cbean : updateList){ pstmt.setString(1, cbean.getLicense_key()); pstmt.setString(2, cbean.getCorporate_name()); pstmt.setString(3, cbean.getIntegrated_classification()); pstmt.setString(4, cbean.getBosshead()); pstmt.setString(5, cbean.getContact_phone()); pstmt.setString(6, cbean.getOrder_frequency()); pstmt.setString(7, cbean.getContact_address()); pstmt.setInt (8, cbean.getUser_id()); pstmt.setInt (9, cbean.getCustomer_id()); pstmt.addBatch(); } int[] tt = pstmt.executeBatch(); System.out.println("update : " + tt.length); //提交,设置事务初始值 conn.commit(); conn.setAutoCommit(true); //插入成功,返回 return tt.length; }catch(SQLException ex){ try{ //提交失败,执行回滚操作 conn.rollback(); }catch (SQLException e) { e.printStackTrace(); System.err.println("updateExistsInfo回滚执行失败!!!"); } ex.printStackTrace(); System.err.println("updateExistsInfo执行失败"); //插入失败返回标志0 return 0; }finally { try{ //关闭资源 if(pstmt != null)pstmt.close(); if(conn != null)conn.close(); }catch (SQLException e) { e.printStackTrace(); System.err.println("资源关闭失败!!!"); } } } /** * 插入数据中没有的customer信息 * @param List<CustomerBean> * @return */ public int insertNewInfo(List<CustomerBean> insertList){ //查询的SQL语句 String sql = "insert into t_customer(CUSTOMER_ID," + "LICENSE_KEY,CORPORATE_NAME,INTEGRATED_CLASSIFICATION,BOSSHEAD,CONTACT_PHONE," + "ORDER_FREQUENCY,CONTACT_ADDRESS,USER_ID,CUSTOMER_NUM,CUSTOMER_CODING," + "INVESTIGATION_TIME,SMS_REC_FLAG,WAP_FLAG,PRICE_GATHERING_FLAG,SOCIETY_STOCK_FLAG," + "REGION_TYPE)" + "VALUES(CUSTOMER.NEXTVAL," + "?,?,?,?,?," + "?,?,?,?,?," + "TO_DATE(?,'YYYY-MM-DD'),?,0,0,0," + "?)" ; //插入需要的数据库对象 Connection conn = null; PreparedStatement pstmt = null; try { conn = new DBSource().getConnection(); //设置事务属性 conn.setAutoCommit(false); pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); for(CustomerBean cbean : insertList){ pstmt.setString(1, cbean.getLicense_key()); pstmt.setString(2, cbean.getCorporate_name()); pstmt.setString(3, cbean.getIntegrated_classification()); pstmt.setString(4, cbean.getBosshead()); pstmt.setString(5, cbean.getContact_phone()); pstmt.setString(6, cbean.getOrder_frequency()); pstmt.setString(7, cbean.getContact_address()); pstmt.setInt(8, cbean.getUser_id()); pstmt.setString(9, "gyyc00000");// pstmt.setString(10, "95000000");// pstmt.setString(11, getToday()); pstmt.setInt(12, cbean.getSms_rec_flag()); pstmt.setInt(13, cbean.getRegion_type()); pstmt.addBatch(); } int[] tt = pstmt.executeBatch(); System.out.println("insert : " + tt.length); //提交,设置事务初始值 conn.commit(); conn.setAutoCommit(true); //插入成功,返回 return tt.length; }catch(SQLException ex){ try{ //提交失败,执行回滚操作 conn.rollback(); }catch (SQLException e) { e.printStackTrace(); System.err.println("insertNewInfo回滚执行失败!!!"); } ex.printStackTrace(); System.err.println("insertNewInfo执行失败"); //插入失败返回标志0 return 0; }finally { try{ //关闭资源 if(pstmt != null)pstmt.close(); if(conn != null)conn.close(); }catch (SQLException e) { e.printStackTrace(); System.err.println("资源关闭失败!!!"); } } } Notice: //设置事务属性 conn.setAutoCommit(false); pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); for(CustomerBean cbean : updateList){ pstmt.setString(1, cbean.getLicense_key()); ... pstmt.addBatch(); } int[] tt = pstmt.executeBatch(); System.out.println("update : " + tt.length); //提交,设置事务初始值 conn.commit(); conn.setAutoCommit(true); ...
相关文章推荐
- 关于oracle启用实例,以及数据库重启
- lnux内核的malloc实现(Oracle的cache buffer影子)
- oracle一个事务的完整流程分析
- oracle事务
- PLSQL连Oracle数据库Could not load "……\bin\oci.dll"
- oracle创建一个用户并授予最高权限
- oracle执行sql plus导入dmp格式数据备份
- LINUX下环境下链接oracle 数据库搭建测试环境
- oracle 数据批量导出工具
- oracle 数据批量导入工具
- Oracle GoldenGate 快速安装配置实用指南
- Oracle客户端配置说明 centos
- oracle学习篇一:sqlplus常用命令
- ora-19624 ora-19502 ora-27040
- 关于oracle用户进程,服务进程,后台进程
- oracle数据字典
- Oracle内存结构:SGA PGA UGA
- Oracle之内存结构(SGA、PGA)
- Oracle程序全局区(PGA)
- Oracle内存全面分析之PGA