JavaWeb ----使用JDBC实现批处理

匿名 (未验证) 提交于 2019-12-02 21:52:03

JDBC实现批处理方式有两种:statement() or preparedstatement()

一.使用preparedstatement()方法实现

package test;  import java.io.InputStream; import java.sql.*; import java.util.Properties; import org.junit.Test;  /**  * JDBC操作  * */ public class JdbcTest {      private static String url;     private static String username;     private static String password;          static {         try {             InputStream is = PropTest.class.getResourceAsStream("/jdbc.properties");             Properties prop = new Properties();             prop.load(is);                          // 将驱动类加载到JVM中             String driver = prop.getProperty("jdbc.driver");             Class.forName(driver);                          url = prop.getProperty("jdbc.url");             username = prop.getProperty("jdbc.username");             password = prop.getProperty("jdbc.password");         } catch(Exception e) {             e.printStackTrace();         }     }          public static void main(String[] args) throws Exception {         Connection conn = DriverManager.getConnection(url, username, password);         System.out.println(conn);     }          // ============= 单元测试 ==============     @Test     public void create() throws Exception {         String sql = "create table tb_user (id int primary key auto_increment, name varchar(30), age int)";         Connection conn = DriverManager.getConnection(url, username, password);         PreparedStatement pst = conn.prepareStatement(sql);         pst.executeUpdate();         pst.close();                  conn.close();     }          @Test     public void insert() throws Exception {         String sql = "insert into tb_user values(null,?,?)";         Connection conn = DriverManager.getConnection(url, username, password);                  for(int i=0; i<10; i++) {             PreparedStatement pst = conn.prepareStatement(sql);             pst.setString(1, "test_" + i);             pst.setInt(2, 20 + i);             pst.executeUpdate();             pst.close();         }         conn.close();     }          @Test     public void update() throws Exception {         String sql = "update tb_user set name=? where id=?";         Connection conn = DriverManager.getConnection(url, username, password);                  PreparedStatement pst = conn.prepareStatement(sql);         pst.setObject(1, "张三");         pst.setObject(2, 1);         pst.executeUpdate();         pst.close();                  conn.close();     }          @Test     public void delete() throws Exception {         String sql = "delete from tb_user where age>=?";         Connection conn = DriverManager.getConnection(url, username, password);                  PreparedStatement pst = conn.prepareStatement(sql);         pst.setObject(1, 28);         pst.executeUpdate();         pst.close();                  conn.close();     }          @Test     public void query1() throws Exception {         String sql = "select * from tb_user where id=?";         Connection conn = DriverManager.getConnection(url, username, password);                  PreparedStatement pst = conn.prepareStatement(sql);         pst.setObject(1, 1);                  ResultSet rs = pst.executeQuery();         if(rs.next()) {             int id = rs.getInt("id");             String name = rs.getString("name");             int age = rs.getInt("age");                          String msg = String.format("id=%d, name=%s, age=%d", id, name, age);             System.out.println(msg);         }                  pst.close();         conn.close();     }          @Test     public void query2() throws Exception {         String sql = "select * from tb_user";         Connection conn = DriverManager.getConnection(url, username, password);                  PreparedStatement pst = conn.prepareStatement(sql);         ResultSet rs = pst.executeQuery();         while(rs.next()) {             int id = rs.getInt("id");             String name = rs.getString("name");             int age = rs.getInt("age");                          String msg = String.format("id=%d, name=%s, age=%d", id, name, age);             System.out.println(msg);         }                  pst.close();         conn.close();     }          /// ========== 批量操作 =============     @Test     public void insert2() throws Exception {         String sql = "insert into tb_user values(null,?,?)";         Connection conn = DriverManager.getConnection(url, username, password);         long begin = System.currentTimeMillis();         for(int i=0; i<100000; i++) {             PreparedStatement pst = conn.prepareStatement(sql);             pst.setString(1, "test_" + i);             pst.setInt(2, 20 + i);             pst.executeUpdate();             pst.close();         }                 conn.close();         System.out.println(System.currentTimeMillis() - begin);     }          // executeBatch()没有效果     @Test     public void insert3() throws Exception {         String sql = "insert into tb_user values(null,?,?)";         Connection conn = DriverManager.getConnection(url, username, password);         long begin = System.currentTimeMillis();         PreparedStatement pst = conn.prepareStatement(sql);         for(int i=0; i<100000; i++) {             pst.setString(1, "test_" + i);             pst.setInt(2, 20 + i);             pst.addBatch();         }         pst.executeBatch();         pst.close();         conn.close();         System.out.println(System.currentTimeMillis() - begin);     }          // executeBatch():必须保证事务不自动提交     @Test     public void insert4() throws Exception {         String sql = "insert into tb_user values(null,?,?)";         Connection conn = DriverManager.getConnection(url, username, password);         conn.setAutoCommit(false);                  long begin = System.currentTimeMillis();         PreparedStatement pst = conn.prepareStatement(sql);         for(int i=0; i<100000; i++) {             pst.setString(1, "test_" + i);             pst.setInt(2, 20 + i);             pst.addBatch();                          // 如果数据量比较大,需要分批次提交             if((i+1)%10000 == 0) {                 pst.executeBatch();                 pst.clearBatch();             }         }         pst.executeBatch();         conn.setAutoCommit(true);                  pst.close();         conn.close();                  System.out.println(System.currentTimeMillis() - begin);     } }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!