JDBC:用Batch处理多个SQL(SXT)

来源:互联网 发布:vb控制鼠标点击 源码 编辑:程序博客网 时间:2024/06/16 07:54

 JDBC:用Batch处理多个SQL(SXT)

 

Demo

 

  1. import java.sql.*;
  2. public class TestBatch
  3. {
  4.     /**
  5.      * @用Batch处理多个SQL
  6.      */
  7.     public static void main(String[] args)
  8.     {
  9.         // 1.不用PreparedStatement
  10.         /*
  11.          * Connection conn = null; Statement stmt = null; String url =
  12.          * "jdbc:mysql://localhost:3306/college"; String user = "root"; String
  13.          * password = "123456";
  14.          * 
  15.          * try { Class.forName("com.mysql.jdbc.Driver"); conn =
  16.          * DriverManager.getConnection(url, user, password); stmt =
  17.          * conn.createStatement(); stmt.addBatch("insert into user
  18.          * values(301,'go1','123','0')"); stmt.addBatch("insert into user
  19.          * values(302,'go2','1234','00')"); stmt.addBatch("insert into user
  20.          * values(303,'go3','12345','000')");
  21.          * 
  22.          * stmt.executeBatch();
  23.          *  } catch (ClassNotFoundException e) { // TODO Auto-generated catch
  24.          * block e.printStackTrace(); } catch (SQLException e) { // TODO
  25.          * Auto-generated catch block e.printStackTrace(); } finally { try {
  26.          * if(stmt != null) { stmt.close(); stmt = null; } if(conn != null) {
  27.          * conn.close(); conn = null;
  28.          *  }
  29.          *  } catch (SQLException e) { // TODO Auto-generated catch block
  30.          * e.printStackTrace(); } }
  31.          *  }
  32.          */
  33.         // 2.用PreparedStatement处理
  34.         Connection conn = null;
  35.         PreparedStatement pstmt = null;
  36.         String url = "jdbc:mysql://localhost:3306/college";
  37.         String user = "root";
  38.         String password = "123456";
  39.         try
  40.         {
  41.             Class.forName("com.mysql.jdbc.Driver");
  42.             conn = DriverManager.getConnection(url, user, password);
  43.             pstmt = conn.prepareStatement("insert into user values(?,?,?,?)");
  44.             pstmt.setInt(1401);
  45.             pstmt.setString(2"goo1");
  46.             pstmt.setString(3"a1");
  47.             pstmt.setString(4"k1");
  48.             pstmt.addBatch();
  49.             pstmt.setInt(1402);
  50.             pstmt.setString(2"goo2");
  51.             pstmt.setString(3"a2");
  52.             pstmt.setString(4"k2");
  53.             pstmt.addBatch();
  54.             pstmt.setInt(1403);
  55.             pstmt.setString(2"goo3");
  56.             pstmt.setString(3"a3");
  57.             pstmt.setString(4"k3");
  58.             pstmt.addBatch();
  59.             pstmt.executeBatch();
  60.         } catch (ClassNotFoundException e)
  61.         {
  62.             // TODO Auto-generated catch block
  63.             e.printStackTrace();
  64.         } catch (SQLException e)
  65.         {
  66.             // TODO Auto-generated catch block
  67.             e.printStackTrace();
  68.         } finally
  69.         {
  70.             try
  71.             {
  72.                 if (pstmt != null)
  73.                 {
  74.                     pstmt.close();
  75.                     pstmt = null;
  76.                 }
  77.                 if (conn != null)
  78.                 {
  79.                     conn.close();
  80.                     conn = null;
  81.                 }
  82.             } catch (SQLException e)
  83.             {
  84.                 // TODO Auto-generated catch block
  85.                 e.printStackTrace();
  86.             }
  87.         }
  88.     }
  89. }