日期:2014-05-20 浏览次数:20702 次
out.println("循环插入数据...<br/>"); long startTime = System.currentTimeMillis(); Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=yiqianmi"; //test你的数据库的 String user = "sa"; String password = "ok"; Connection conn = DriverManager.getConnection(url,user,password); conn.setAutoCommit(false); PreparedStatement pst = (PreparedStatement)conn.prepareStatement("insert into batch_index values (?,?,?,?)"); int addNum = 100000; int startPos = 0; int endPos = addNum+startPos; out.println("插入"+addNum+"条数据<br>"); java.util.Date curDate = new java.util.Date(); java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm"); String curDateStr = sdf.format(curDate); System.out.println("循环插入"+addNum+"条数据..."); for(int i=startPos+1; i<=endPos; i++){ pst.setInt(1,i); pst.setInt(2,i+100); pst.setString(3,new String(("第"+i+"班名称").getBytes("GBK"),"iso8859-1")); pst.setString(4, curDateStr); pst.execute(); } conn.commit(); System.out.println("================================================"); System.out.println("插入成功"); conn.close(); long endTime = System.currentTimeMillis(); out.println("消耗时间:"+(endTime-startTime)+"ms");
out.println("批量插入数据...<br/>"); long startTime = System.currentTimeMillis(); Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;SelectMethod=cursor;DatabaseName=yiqianmi"; //test你的数据库的 String user = "sa"; String password = "ok"; Connection conn = DriverManager.getConnection(url,user,password); conn.setAutoCommit(false); PreparedStatement pst = (PreparedStatement)conn.prepareStatement("insert into batch_index values (?,?,?,?)"); int addNum = 100000; int startPos = 0; int endPos = addNum+startPos; out.println("插入"+addNum+"条数据<br>"); java.util.Date curDate = new java.util.Date(); java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm"); String curDateStr = sdf.format(curDate); System.out.println("批量插入"+addNum+"条数据..."); for(int i=startPos+1; i<=endPos; i++) { pst.setInt(1,i); pst.setInt(2,i+100); pst.setString(3,new String(("第"+i+"班名称").getBytes("GBK"),"iso8859-1")); pst.setString(4,curDateStr); pst.addBatch(); if(i % 100 == 0){ pst.executeBatch(); } } pst.executeBatch(); conn.commit(); System.out.println("================================================"); System.out.println("插入成功"); conn.close(); long endTime = System.currentTimeMillis(); out.println("消耗时间:"+(endTime-startTime)+"ms");