jdbc操作测试类

    技术2022-06-12  49

    package com.fs.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; import java.util.Random; public class Test2 { public static String getNumber() { Timestamp now = new Timestamp(System.currentTimeMillis()); String number = now.toString().replace("-", "").replace(" ", "") .replace(":", "").replace(".", ""); String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Random r = new Random(); for (int i = 0; i < 2; i++) { int n = r.nextInt(26); number = str.substring(n, n + 1) + number; } return number; } public void exec3(Connection conn, String sql) { try { conn.setAutoCommit(false); Long beginTime = System.currentTimeMillis(); PreparedStatement pst = conn.prepareStatement(sql); Statement st = conn.createStatement(); for (int i = 1; i <=22; i++) { long num = 13871124811l + i; System.out.println("num="+num); pst.setString(1, String.valueOf(num)); pst.addBatch(); if (i % 1000 == 0) {// 可以设置不同的大小;如50,100,500,1000等等 System.out.println("==可以设置不同的大小;如50,100,500,1000等等=="); pst.executeBatch(); conn.commit(); pst.clearBatch(); }else if(i==22){ System.out.println("==1000000=="); pst.executeBatch(); conn.commit(); pst.clearBatch(); } } // st.executeUpdate(sql); Long endTime = System.currentTimeMillis(); System.out.println("pst+batch:" + (endTime - beginTime) / 1000 + "秒"); conn.commit(); st.close(); pst.close(); conn.close(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public static void main(String[] args) { // System.out.println(Test2.getNumber()); try { Class.forName("com.ibm.db2.jcc.DB2Driver"); Connection conn = DriverManager.getConnection( "jdbc:db2://192.168.1.1:50110/masdb", "name", "paswd"); String sql1= "INSERT INTO TBL_SMSENDTASK (CREATORID,TASKNAME,SMSENDEDNUM,OPERATIONTYPE," + "SUBOPERATIONTYPE,SENDTYPE,ORGADDR,DESTADDR,SM_CONTENT,SENDTIME, " + "NEEDSTATEREPORT,SERVICEID,FEETYPE,FEECODE,MSGID,SMTYPE,MESSAGEID, " + "DESTADDRTYPE,SUBTIME,TASKSTATUS,SENDLEVEL,SENDSTATE,TRYTIMES,COUNT," + "SUCCESSID,RESERVE1,RESERVE2,SISMSID,MSGFMT,SENDMETHODTYPE,APPLICATIONID," + "SOURCEFLAG) VALUES" + "('0000','ff',0,'WAS','66',1,'1111111',?," + "'这是测试,谢谢!','2011-05-31 15:43:15',0,'bbbbb','01'," + "'10',null,0,'0',0,'2011-05-31 15:43:15',0,2,0,3,1,0,null,null,null,null,0,null,0)"; Test2 te=new Test2(); te.exec3(conn,sql1); } catch (Exception e) { e.printStackTrace(); } } }


    最新回复(0)