jdbc批量插入

jdbc批量插入, 一定要关闭事物的自动提交, 否则效率会非常慢, 对于插入出现异常的一批数据, 暂时做整体回滚操作!


[java]  view plain  copy
  1. package com.thinkive.import_data.utils;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8. import java.sql.Timestamp;  
  9. import java.text.SimpleDateFormat;  
  10. import java.util.ArrayList;  
  11. import java.util.Date;  
  12. import java.util.List;  
  13. import java.util.ResourceBundle;  
  14.   
  15. import oracle.sql.DATE;  
  16.   
  17. import org.apache.log4j.Logger;  
  18.   
  19. import com.thinkive.import_data.bean.Bean;  
  20.   
  21. /** 
  22.  * @time: 2016年11月29日 下午12:33:41 
  23.  */  
  24. public class DBHelp {  
  25.   
  26.     private static Logger logger = Logger.getLogger(DBHelp.class);  
  27.     private static Connection conn;  
  28.     private static PreparedStatement ps;  
  29.     private static SimpleDateFormat format = new SimpleDateFormat(Constants.DATE_FORMATE_yyyyMMddHHmmss);  
  30.     public static final String SQL_INSTALL_IDNO_THIRD = "INSERT INTO t_stkkh_idno_third (ID, IDNO, NAME, VERIFY_RESULT, VERIFY_TIME, CHANNEL_TYPE, CREATE_TIME, UPDATE_TIME) VALUES (seq_stkkh_idno_third.nextval, ?,?,?,?,?,?,?)";  
  31.   
  32.     /** 
  33.      * 获取连接对象 
  34.      * @return 连接对象 
  35.      * @throws Exception  
  36.      */  
  37.     private static Connection getConnection() throws Exception {  
  38.         // 在项目根目录下配置oracle.properties文件, 配置url,username,password; <span style="font-family: Arial, Helvetica, sans-serif;">ResourceBundle jdk1.7中可以用来读取配置文件的类</span>  
  39.         ResourceBundle rb = ResourceBundle.getBundle("oracle");  
  40.         Class.forName("oracle.jdbc.driver.OracleDriver");  
  41.         String url = rb.getString("url");  
  42.         conn = DriverManager.getConnection(url, rb.getString("username"), rb.getString("password"));  
  43.         return conn;  
  44.   
  45.     }  
  46.   
  47.     /** 
  48.      * 执行增删改操作  
  49.      * @param sql语句 
  50.      * @param beans封装bean的集合 
  51.      * @param pattern 日期格式 
  52.      * @return int 影响行数  
  53.      * @throws Exception  
  54.      */  
  55.     public static int[] executeUpate(String sql,List<Bean>  beans, String pattern) throws Exception{  
  56.           
  57.         if(pattern !=null && pattern.length() > 0) {  
  58.             format = new SimpleDateFormat(pattern);  
  59.         }  
  60.         if(conn == null) {  
  61.             conn=getConnection();  
  62.         }  
  63.         // 关闭事物自动提交  
  64.         conn.setAutoCommit(false);  
  65.         try {  
  66.             ps=conn.prepareStatement(sql);  
  67.             //注入参数  
  68.             if(beans!=null && beans.size() > 0){  
  69.                 int k = 1;  
  70.                 for (Bean bean : beans) {  
  71.                     java.sql.Timestamp date = new java.sql.Timestamp(new Date().getTime());  
  72.                     ps.setObject(k++, bean.getIdno());  
  73.                     ps.setObject(k++, bean.getName());  
  74.                     ps.setObject(k++, bean.getVerify_result());  
  75.                     ps.setTimestamp(k++, new java.sql.Timestamp(format.parse(bean.getVerify_time()).getTime()));  
  76.                     ps.setObject(k++, bean.getChannel_type());  
  77.                     ps.setTimestamp(k++, date);  
  78.                     ps.setTimestamp(k++, date);  
  79.                     k = 1;  
  80.                     // 添加到批处理  
  81.                     ps.addBatch();  
  82.                 }  
  83.             }  
  84.             int[] res = ps.executeBatch();  
  85.             conn.commit();  
  86.             return res;  
  87.         } catch (SQLException e) {  
  88.             conn.rollback();   
  89.             logger.error("部分数据导入失败", e);  
  90.         }finally {  
  91.             ps.clearBatch();  
  92.             if(ps != null) {  
  93.                 ps.close();  
  94.                 ps = null;  
  95.             }  
  96.         }  
  97.         return new int[] {0};  
  98.     }  
  99.   
  100.   
  101.     /** 
  102.      * 关闭资源 
  103.      */  
  104.     public static void closeSources(Connection conn, PreparedStatement ps){  
  105.   
  106.         try {  
  107.             if (ps != null) {  
  108.                 ps.close();  
  109.                 ps = null;  
  110.             }  
  111.             if (conn != null) {  
  112.                 conn.close();  
  113.                 conn = null;  
  114.             }  
  115.         } catch (SQLException e) {  
  116.             logger.error("数据库关闭失败");  
  117.         }  
  118.     }  
  119.       
  120.       
  121.   
  122.     public static Connection getConn() {  
  123.         return conn;  
  124.     }  
  125.   
  126.     public static PreparedStatement getPs() {  
  127.         return ps;  
  128.     }  
  129.   
  130.       
  131.     public static void main(String[] args) throws Exception {  
  132.         List<Bean> beans = new ArrayList<Bean>();  
  133.         beans.add(new Bean());  
  134.         beans.add(new Bean());  
  135.         beans.add(new Bean());  
  136.         // 批量插入  
  137.         executeUpate(SQL_INSTALL_IDNO_THIRD, beans, "");  
  138.         // 关闭链接  
  139.         closeSources(conn, ps);  
  140.     }  
  141. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值