- /**
- *更新数据库已有的customer信息
- *@paramList<CustomerBean>
- *@return
- */
- publicintupdateExistsInfo(List<CustomerBean>updateList){
- //查询的sql语句
- Stringsql="updatet_customersetLICENSE_KEY=?,CORPORATE_NAME=?,INTEGRATED_CLASSIFICATION=?,BOSSHEAD=?,"+
- "CONTACT_PHONE=?,ORDER_FREQUENCY=?,CONTACT_ADDRESS=?,USER_ID=?whereCUSTOMER_ID=?";
- //插入需要的数据库对象
- Connectionconn=null;
- PreparedStatementpstmt=null;
- try{
- conn=newDBSource().getConnection();
- //设置事务属性
- conn.setAutoCommit(false);
- pstmt=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
- for(CustomerBeancbean:updateList){
- pstmt.setString(1,cbean.getLicense_key());
- pstmt.setString(2,cbean.getCorporate_name());
- 3,cbean.getIntegrated_classification());
- 4,cbean.getBosshead());
- 5,cbean.getContact_phone());
- 6,cbean.getOrder_frequency());
- 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);
- //插入成功,返回
- returntt.length;
- }catch(sqlExceptionex){
- //提交失败,执行回滚操作
- conn.rollback();
- }catch(sqlExceptione){
- e.printStackTrace();
- System.err.println("updateExistsInfo回滚执行失败!!!");
- }
- ex.printStackTrace();
- System.err.println("updateExistsInfo执行失败");
- //插入失败返回标志0
- return0;
- finally{
- //关闭资源
- if(pstmt!=null)pstmt.close();
- if(conn!=null)conn.close();
- catch(sqlExceptione){
- e.printStackTrace();
- System.err.println("资源关闭失败!!!");
- /**
- *插入数据中没有的customer信息
- *@paramList<CustomerBean>
- *@return
- */
- intinsertNewInfo(List<CustomerBean>insertList){
- Stringsql="insertintot_customer(CUSTOMER_ID,108); list-style-type:decimal-leading-zero; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> "LICENSE_KEY,CORPORATE_NAME,INTEGRATED_CLASSIFICATION,BOSSHEAD,CONTACT_PHONE,"+
- "ORDER_FREQUENCY,CONTACT_ADDRESS,USER_ID,CUSTOMER_NUM,CUSTOMER_CODING,108); list-style-type:decimal-leading-zero; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> "INVESTIGATION_TIME,SMS_REC_FLAG,WAP_FLAG,PRICE_GATHERING_FLAG,SOCIETY_STOCK_FLAG,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> "REGION_TYPE)"+
- "VALUES(CUSTOMER.NEXTVAL,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> "?,?,108); list-style-type:decimal-leading-zero; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> "?,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> "TO_DATE(?,'YYYY-MM-DD'),108); list-style-type:decimal-leading-zero; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; list-style-position:outside!important"> "?)";
- //插入需要的数据库对象
- Connectionconn= PreparedStatementpstmt=try{
- conn=newDBSource().getConnection();
- pstmt=conn.prepareStatement(sql,ResultSet.CONCUR_READ_ONLY);
- for(CustomerBeancbean:insertList){
- "gyyc00000");//
- 10,"95000000");//
- 11,getToday());
- 12,cbean.getSms_rec_flag());
- 13,cbean.getRegion_type());
- pstmt.addBatch();
- int[]tt=pstmt.executeBatch();
- System.out.println("insert:"+tt.length);
- //提交,设置事务初始值
- conn.commit();
- true);
- //插入成功,返回
- returntt.length;
- catch(sqlExceptionex){
- //提交失败,执行回滚操作
- conn.rollback();
- System.err.println("insertNewInfo回滚执行失败!!!");
- ex.printStackTrace();
- System.err.println("insertNewInfo执行失败");
- //插入失败返回标志0
- 0;
- finally{
- //关闭资源
- null)pstmt.close();
- null)conn.close();
- System.err.println("资源关闭失败!!!");
- }
使用Java JDBC基本的API批量插入数据到数据库中
- importjava.sql.Connection;
- importjava.sql.Statement;
- //...
- Connectionconnection=newgetConnection();
- Statementstatemenet=connection.createStatement();
- for(Employeeemployee:employees){
- Stringquery="insertintoemployee(name,city)values('"
- +employee.getName()+"','"+employee.getCity+"')";
- statemenet.addBatch(query);
- }
- statemenet.executeBatch();
- statemenet.close();
- connection.close();
-
请注意我们是如何从Employee对象中的数据动态创建查询并在批处理中添加,插入一气呵成。完美!是不是?
等等......你必须思考什么关于sql注入?这样动态创建的查询sql注入是很容易的。并且每个插入查询每次都被编译。
为什么不使用PreparedStatement而不是简单的声明。是的,这是个解决方案。下面是sql注入安全批处理。
sql Injection Safe Batch - sql注入安全批处理
思考一下下面代码:
看看上面的 代码。漂亮。我们使用的java.sql.PreparedStatement和在批处理中添加INSERT查询。这是你必须实现批量插入逻辑的解决方案,而不是上述Statement那个。
这一解决方案仍然存在一个问题。考虑这样一个场景,在您想要插入到数据库使用批处理上万条记录。嗯,可能产生的OutOfMemoryError:
java.lang.OutOfMemoryError: Java heap space
com.MysqL.jdbc.ServerPreparedStatement$BatchedBindValues.<init>(ServerPreparedStatement.java:72)
com.MysqL.jdbc.ServerPreparedStatement.addBatch(ServerPreparedStatement.java:330)
org.apache.commons.dbcp.DelegatingPreparedStatement.addBatch(DelegatingPreparedStatement.java:171)
这是因为你试图在一个批次添加所有语句,并一次插入。最好的办法是将执行分批次。看看下面的解决方案
Smart Insert: Batch within Batch - 智能插入:将整批分批
这是一个简单的解决方案。考虑批量大小为1000,每1000个查询语句为一批插入提交。
copy- Stringsql="insertintoemployee(name,?)";
- newgetConnection();
- PreparedStatementps=connection.prepareStatement(sql);
- finalintbatchSize=1000;
- intcount=0;
- for(Employeeemployee:employees){
- ps.addBatch();
- if(++count%batchSize==0){
- ps.executeBatch();
- }
- ps.executeBatch();//insertremainingrecords
- connection.close();