JDBC:用Batch处理多个SQL(SXT)
来源:互联网 发布:vb控制鼠标点击 源码 编辑:程序博客网 时间:2024/06/16 02:07
JDBC:用Batch处理多个SQL(SXT)
Demo
- import java.sql.*;
- public class TestBatch
- {
- /**
- * @用Batch处理多个SQL
- */
- public static void main(String[] args)
- {
- // 1.不用PreparedStatement
- /*
- * Connection conn = null; Statement stmt = null; String url =
- * "jdbc:mysql://localhost:3306/college"; String user = "root"; String
- * password = "123456";
- *
- * try { Class.forName("com.mysql.jdbc.Driver"); conn =
- * DriverManager.getConnection(url, user, password); stmt =
- * conn.createStatement(); stmt.addBatch("insert into user
- * values(301,'go1','123','0')"); stmt.addBatch("insert into user
- * values(302,'go2','1234','00')"); stmt.addBatch("insert into user
- * values(303,'go3','12345','000')");
- *
- * stmt.executeBatch();
- * } catch (ClassNotFoundException e) { // TODO Auto-generated catch
- * block e.printStackTrace(); } catch (SQLException e) { // TODO
- * Auto-generated catch block e.printStackTrace(); } finally { try {
- * if(stmt != null) { stmt.close(); stmt = null; } if(conn != null) {
- * conn.close(); conn = null;
- * }
- * } catch (SQLException e) { // TODO Auto-generated catch block
- * e.printStackTrace(); } }
- * }
- */
- // 2.用PreparedStatement处理
- Connection conn = null;
- PreparedStatement pstmt = null;
- String url = "jdbc:mysql://localhost:3306/college";
- String user = "root";
- String password = "123456";
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(url, user, password);
- pstmt = conn.prepareStatement("insert into user values(?,?,?,?)");
- pstmt.setInt(1, 401);
- pstmt.setString(2, "goo1");
- pstmt.setString(3, "a1");
- pstmt.setString(4, "k1");
- pstmt.addBatch();
- pstmt.setInt(1, 402);
- pstmt.setString(2, "goo2");
- pstmt.setString(3, "a2");
- pstmt.setString(4, "k2");
- pstmt.addBatch();
- pstmt.setInt(1, 403);
- pstmt.setString(2, "goo3");
- pstmt.setString(3, "a3");
- pstmt.setString(4, "k3");
- pstmt.addBatch();
- pstmt.executeBatch();
- } catch (ClassNotFoundException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally
- {
- try
- {
- if (pstmt != null)
- {
- pstmt.close();
- pstmt = null;
- }
- if (conn != null)
- {
- conn.close();
- conn = null;
- }
- } catch (SQLException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- JDBC:用Batch处理多个SQL(SXT)
- JDBC处理DML(SXT)
- JDBC中处理事务(SXT)
- JDBC处理前后移动结果集(SXT)
- JDBC:灵活指定SQL中的变量(SXT)
- JDBC进阶(SXT)
- 批处理batch,执行多个SQL语句
- 批处理batch,执行多个SQL语句
- 批处理batch,执行多个SQL语句
- 批处理Batch,执行多个Sql语句
- JDBC调用存储过程基本流程(SXT)
- 对多个日期排序,然后用二分查找法查找相应的日期(SXT)
- JDBC批量插入(batch)
- jdbc batch
- JDBC batch
- 用二分查找法对数组查找(SXT)
- jdbc batch批量处理与非批量的比较
- SXT:JDBC超级入门——JDBC编程步骤
- 小於正整數n的任意個正整數相加其和等於n的做法
- JDBC处理DML(SXT)
- [职业生涯] 28句话让你的人际关系更上一层楼!
- 构建ARM Linux系统实践
- buffalo.js文件分析(一)
- JDBC:用Batch处理多个SQL(SXT)
- 如何培养女儿爱护东西的好习惯?
- 傲游今天出现问题
- Windows2000 在保存IP安全数据时出现拒绝访问错误80070005的解决方法
- 使用 Java 开源工具建立一个灵活的搜索引擎
- JDBC:灵活指定SQL中的变量(SXT)
- VC多线程编程
- 約瑟夫環解法
- [招聘]我司求 python 或 flex 程序员各一名