java web批量插入OracleJdbc

    技术2022-07-10  148

    package com.slip.controller.ws;

    import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Properties;

    import org.springframework.web.multipart.MultipartFile;

    import com.pt.utils.DESUtil;

    import oracle.jdbc.OracleTypes;

    public class OracleJdbc {          //数据连接     private String USERNAMR = "登录名";     private String PASSWORD = "密码";     private String DRVIER = "oracle.jdbc.OracleDriver";     private String URL = "jdbc:oracle:thin:@IP:端口:orcl";

        // 创建一个数据库连接     Connection connection = null;     // 创建预编译语句对象,一般都是用这个而不用Statement     PreparedStatement pstm = null;     // 创建一个结果集对象     ResultSet rs = null;          /**      * 获取Connection对象      *       * @return      */     public Connection getConnection() {                  Properties prop = new Properties();          try {                  //读取配置文件                 prop.load(this.getClass().getClassLoader().getResourceAsStream("properties/db.properties"));                 URL = prop.getProperty("jdbc.url");                 USERNAMR = DESUtil.getDecryptString(prop.getProperty("jdbc.username"));                 PASSWORD = DESUtil.getDecryptString(prop.getProperty("jdbc.password"));                 DRVIER = prop.getProperty("jdbc.driver");             } catch (IOException e) {                 e.printStackTrace();             }                  if (connection == null) {             synchronized (OracleJdbc.class) {                 if (connection == null) {                     try {                         Class.forName(DRVIER);                         connection = DriverManager.getConnection(URL, USERNAMR, PASSWORD);                         // System.out.println("成功连接数据库");                     } catch (ClassNotFoundException e) {                         throw new RuntimeException("class not find !", e);                     } catch (SQLException e) {                         throw new RuntimeException("get connection error!", e);                     }                 }             }         }         return connection;     }

        /**      * 释放资源      */     public void ReleaseResource() {         if (rs != null) {             try {                 rs.close();             } catch (SQLException e) {                 e.printStackTrace();             }         }         if (pstm != null) {             try {                 pstm.close();             } catch (SQLException e) {                 e.printStackTrace();             }         }         if (connection != null) {             try {                 connection.close();             } catch (SQLException e) {                 e.printStackTrace();             }         }     }          //导入数据       public String impTxt(List<MultipartFile> list, String path) {           String flag = "1";           System.gc(); //清缓存           long startTime = System.currentTimeMillis();           long addtime = 0;           Connection conn = null;           try {               conn = getConnection();             conn.setAutoCommit(false);             Boolean snap = true;             String str = "";             InputStream is = null;             BufferedReader br = null;                          PreparedStatement stmt = conn.prepareStatement("insert into wms_sort_temp_ord(order_code,path_code,path_name,cust_code,cust_name,address,cust_seq,principal,tele_phone,product_code,product_name,qty,price,amount_sum,order_date," +                        "deli_man_code,deli_man_name,sell_license_code,sort_date,box_qty,state,temp1,txt_name)" +                        "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");                          //读取txt文件数据             //插入数据               int num = 0;             for(MultipartFile mFile : list) {                 // 获取TxT文件                 is = new FileInputStream(path + mFile.getOriginalFilename());                 //转为字节流                 br = new BufferedReader(new InputStreamReader(is, "gbk"));                                  //插入数据                 while ((str = br.readLine()) != null) {                       String line = (String) str;                       String[] temp = line.split(",");                       if(snap) {                         //删除重复导入                         PreparedStatement stmtDel = conn.prepareStatement("delete wms_sort_temp_ord where path_code = ? and order_date = ? and txt_name = ?");                         stmtDel.setString(1, temp[1]);                         stmtDel.setString(2, temp[13]);                         stmtDel.setString(3, mFile.getOriginalFilename());                         stmtDel.executeUpdate();                         snap = false;                     }                     stmt.setString(1, temp[16]);                     stmt.setString(2, temp[1]);                     stmt.setString(3, temp[2]);                     stmt.setString(4, temp[3]);                     stmt.setString(5, temp[4]);                     stmt.setString(6, temp[5]);                     stmt.setString(7, temp[0]);                     stmt.setString(8, temp[6]);                     stmt.setString(9, temp[7]);                     stmt.setString(10, temp[8]);                     stmt.setString(11, temp[9]);                     stmt.setString(12, temp[10]);                     stmt.setString(13, temp[11]);                     stmt.setString(14, temp[12]);                     stmt.setString(15, temp[13]);                     stmt.setString(16, temp[14]);                     stmt.setString(17, temp[15]);                     stmt.setString(18, temp[18]);                     stmt.setString(19, temp[19]);                     stmt.setString(20, temp[20]);                     stmt.setString(21, "0");                     stmt.setString(22, temp[17]);                     stmt.setString(23, mFile.getOriginalFilename());                     stmt.addBatch();                     num ++;                     //每5万,提交一次                     if(num > 50000){                         stmt.executeBatch();                         stmt.clearBatch(); // 清空缓存                         conn.commit();                         num = 0;                     }                 }                 br.close();                 snap = true;             }             stmt.executeBatch();               conn.commit();           } catch (Exception e) {               flag = "0";               e.printStackTrace();           } finally {               long endTime =  System.currentTimeMillis();               addtime = (endTime - startTime);               ReleaseResource();           }           return flag + "#" + String.valueOf(addtime);       }              //存储过程调用       public String procedure() {           Connection conn = null;           try {               conn = getConnection();               CallableStatement cs = conn.prepareCall("call pack_sort_ord.sp_sort_synord(?,?,?)");               cs.setString(1, "");               cs.registerOutParameter(2, OracleTypes.NUMBER);               cs.registerOutParameter(3, OracleTypes.VARCHAR);               cs.execute();                          }catch (Exception e) {                        }                      ReleaseResource();           return "";                  }      }  

    Processed: 0.012, SQL: 9