动力节点笔记

 

 
  
  1. import java.sql.*;  
  2. //采用Batch添加数据  
  3. public class BatchTest01 {  
  4.     public static void main(String[] args) {  
  5.         Connection conn = null;  
  6.         PreparedStatement pstmt = null;  
  7.         try {  
  8.             //第一步,加载数据库驱动,不同的数据库驱动程序不一样  
  9.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  10.             //第二部,得到数据库连接  
  11.             String dburl = "jdbc:oracle:thin:@localhost:1521:orcl";  
  12.             //String dburl = "jdbc:oracle:thin:@192.168.21.1:1521:orcl";  
  13.             //String dburl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";  
  14.             String userName = "system";  
  15.             String password = "wanwan";  
  16.             conn = DriverManager.getConnection(dburl, userName, password);  
  17.             //System.out.println(conn);  
  18.               
  19.             String sql = "insert into enp(empno, ename) values(?, ?)";  
  20.             pstmt = conn.prepareStatement(sql);  
  21.             for (int i = 0; i < 10; i++) {        
  22.                 pstmt.setInt(18000 + i);  
  23.                 pstmt.setString(2"赵" + i);  
  24.                 //这种方式与数据库打交道的次数比较频繁  
  25.                 //如果数据库与应用程序不再一台机器上,那么会有性能的损耗  
  26.                 //所以跨网络的传输最好粒度粗一点  
  27.                 pstmt.executeUpdate();  
  28.             }  
  29.               
  30.             System.out.println("添加员工成功");     
  31.               
  32.         } catch (ClassNotFoundException e) {  
  33.             e.printStackTrace();  
  34.         } catch (SQLException e) {  
  35.             e.printStackTrace();  
  36.         } finally {  
  37.             //注意关闭原则:从里到外  
  38.             try {  
  39.                 if (pstmt != null) {  
  40.                     pstmt.close();    
  41.                 }  
  42.                 if (conn != null) {  
  43.                     conn.close();  
  44.                 }  
  45.             } catch(SQLException e) {  
  46.                           
  47.             }  
  48.               
  49.         }  
  50.     }     
  51. }  

 

 

 
  
  1. import java.sql.*;  
  2. //采用Batch添加数据  
  3. public class BatchTest02 {  
  4.     public static void main(String[] args) {  
  5.         Connection conn = null;  
  6.         PreparedStatement pstmt = null;  
  7.         try {  
  8.             //第一步,加载数据库驱动,不同的数据库驱动程序不一样  
  9.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  10.             //第二部,得到数据库连接  
  11.             String dburl = "jdbc:oracle:thin:@localhost:1521:orcl";  
  12.             //String dburl = "jdbc:oracle:thin:@192.168.21.1:1521:orcl";  
  13.             //String dburl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";  
  14.             String userName = "system";  
  15.             String password = "wanwan";  
  16.             conn = DriverManager.getConnection(dburl, userName, password);  
  17.             //System.out.println(conn);  
  18.               
  19.             String sql = "insert into enp(empno, ename) values(?, ?)";  
  20.             pstmt = conn.prepareStatement(sql);  
  21.             for (int i = 1; i <= 100; i++) {          
  22.                 pstmt.setInt(19000 + i);  
  23.                 pstmt.setString(2"张_" + i);  
  24.                 pstmt.addBatch();  
  25.                 if (i % 10 == 0) {  
  26.                     //批量更新  
  27.                     pstmt.executeBatch();  
  28.                 }  
  29.             }  
  30.             pstmt.executeUpdate();  
  31.             System.out.println("添加员工成功");     
  32.               
  33.         } catch (ClassNotFoundException e) {  
  34.             e.printStackTrace();  
  35.         } catch (SQLException e) {  
  36.             e.printStackTrace();  
  37.         } finally {  
  38.             //注意关闭原则:从里到外  
  39.             try {  
  40.                 if (pstmt != null) {  
  41.                     pstmt.close();    
  42.                 }  
  43.                 if (conn != null) {  
  44.                     conn.close();  
  45.                 }  
  46.             } catch(SQLException e) {  
  47.                           
  48.             }  
  49.               
  50.         }  
  51.     }