批量插入 jdbc 把i格式化成num位的字串,不足的位補0;
阿新 • • 發佈:2019-02-14
@Override public void addBillDetail(FmBillMainEntity fmBillMain) { // 新增單據 int startNum = Integer.parseInt(fmBillMain.getStartNum().replaceAll("^(0+)", "")); String prefix = fmBillMain.getBillCodePrefix(); String codeLength = fmBillMain.getBillCodeLength(); int buyCout = fmBillMain.getBuyItemCount(); //List codeList = new ArrayList(); /*int num=Integer.parseInt(codeLength);//編號的位數 for(int i=startNum;i<startNum+buyCout;i++){//要輸出的編號個數為100個,從001........100 System.out.println(prefix+String.format("%0"+num+"d", i));//格式化字串,把i格式化成num位的字串,不足的位補0;例:String.format("%05d",123);結果為“00123” }*/ /* int num=3;//編號的位數 for(int i=1;i<=100;i++){//要輸出的編號個數為100個,從001........100 System.out.println("b"+String.format("%0"+num+"d", i));//格式化字串,把i格式化成num位的字串,不足的位補0;例:String.format("%05d",123);結果為“00123” }*/ BaseDaoUtil b = new BaseDaoUtil(); String sql ="insert into fm_bill ("+ " id,create_name,create_by,create_date,sys_org_code,sys_company_code,bpm_status,bill_type_code,bill_type_name," + "bill_code_prefix,bill_code,bill_status,pk_fm_bill_type_id) values(?,?,?,?,?,?,?,?,?,?,?,?,?)"; PreparedStatement ps = null; Connection conn = b.getCon(); try { ps = conn.prepareStatement(sql); conn.setAutoCommit(false); int num=Integer.parseInt(codeLength);//編號的位數 int listSize = startNum+buyCout; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); HttpSession session = ContextHolderUtils.getSession(); TSUser u = (TSUser) session.getAttribute(ResourceUtil.LOCAL_CLINET_USER); for(int i=startNum;i<listSize;i++){//要輸出的編號個數為100個,從001........100 //System.out.println(prefix+String.format("%0"+num+"d", i));//格式化字串,把i格式化成num位的字串,不足的位補0;例:String.format("%05d",123);結果為“00123” String code = String.format("%0"+num+"d", i); int j =1; ps.setObject(j++, String.valueOf(UUID.randomUUID()).replace("-", "")); ps.setObject(j++, u.getCreateName()); ps.setObject(j++, u.getCreateBy()); ps.setObject(j++, sdf.format(date)); ps.setObject(j++, "systemOrgCode"); ps.setObject(j++, "systemCompanyCode"); ps.setObject(j++, "1"); ps.setObject(j++, fmBillMain.getBillTypeCode()); ps.setObject(j++, fmBillMain.getBillTypeName()); ps.setObject(j++, prefix); ps.setObject(j++, code); ps.setObject(j++, "1");// 購入狀態 ps.setObject(j++, fmBillMain.getBillTypeId());// 購入狀態 ps.addBatch(); if(listSize%5000 == 0){ ps.executeBatch(); conn.commit(); ps.clearBatch(); } } ps.executeBatch(); conn.commit(); ps.clearBatch(); } catch (SQLException e) { e.printStackTrace(); }finally{ if(ps != null){ try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); int listSize = list.size(); Object object[] = null; con = getCon(); con.setAutoCommit(false);//關閉事務自動提交 String sql = "INSERT INTO f_area(administrative_code, parent_administrative_code, grade, area_name, create_time) VALUES(?,?,?,?,?)"; ps = con.prepareStatement(sql); System.out.println("---------總記錄數為:"+list.size()+"-------------"); for(int i = 0;i < listSize;i++){ object = (Object[])list.get(i); ps.setObject(1, object[0]); ps.setObject(2, object[1]); ps.setObject(3, object[2]); ps.setObject(4, object[3]); ps.setObject(5, sdf.format(date)); ps.addBatch(); //把一個SQL命令加入命令列表 if(i != 0 && i % 10 == 0){ System.out.println("=====執行了"+i+"條記錄====="); } if(i == listSize - 1 ){ System.out.println("=====總共成功執行了"+listSize+"條記錄====="); } } ps.executeBatch();//執行批量更新 con.commit();//語句執行完畢,提交本事務 closeAll(rs, ps, con);*/ // 如果型別被使用過不能刪除。現做狀態的修改。bpmStatus /當是2的時禁止刪除標識 String hql = "update FmBillTypeEntity as fbt set fbt.bpmStatus ='2' where id ='" +fmBillMain.getBillTypeId()+"'"; this.executeHql(hql); }