java JDBC最基本的操作读取、调用存储过程、执行批处理、事务等

package test.jdbc;

import java.sql.*;

public class JDBCTest {

 private static Connection conn = null;
 static{
  try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 //最基本的操作读取
 public void testStmt(){
    ResultSet rs = null; 
    Statement stmt = null; 
    try {
    stmt = conn.createStatement(); 
    rs = stmt.executeQuery("select * from dept"); 
    while(rs.next()) { 
         System.out.println(rs.getString("deptno")); 
         System.out.println(rs.getInt("deptno")); 
    } 
    rs.close(); 
    stmt.close(); 
    conn.close();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 
 }
 //PreparedStatement读取
 public void testPrepStmt(){
  PreparedStatement pstmt = null; 
  try {
   pstmt = conn.prepareStatement("insert into dept2 values (?, ?, ?)"); 
   pstmt.setInt(1, 33); 
   pstmt.setString(2, "name"); 
   pstmt.setString(3, "loc"); 
   pstmt.executeUpdate(); 
   pstmt.close(); 
   conn.close(); 
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 
 }
 //调用存储过程
 public void testProc(){
  CallableStatement cstmt;
  try {
   cstmt = conn.prepareCall("{call p(?, ?, ?, ?)}");
   cstmt.registerOutParameter(3, Types.INTEGER); 
   cstmt.registerOutParameter(4, Types.INTEGER); 
   cstmt.setInt(1, 3); 
   cstmt.setInt(2, 4); 
   cstmt.setInt(4, 5); 
   cstmt.execute(); 
   System.out.println(cstmt.getInt(3)); 
   System.out.println(cstmt.getInt(4)); 
   cstmt.close(); 
   conn.close(); 
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 
 }
 //执行批处理
 public void testBatch(){
   /*
  9.         Statement stmt = conn.createStatement();
  10.         stmt.addBatch("insert into dept2 values (51, '500', 'haha')");
  11.         stmt.addBatch("insert into dept2 values (52, '500', 'haha')");
  12.         stmt.addBatch("insert into dept2 values (53, '500', 'haha')");
  13.         stmt.executeBatch();
  14.         stmt.close();
  15.         */ 
  
   
   try {
   PreparedStatement ps = conn.prepareStatement("insert into dept2 values (?, ?, ?)");
   ps.setInt(1, 61);
    ps.setString(2, "haha"); 
    ps.setString(3, "bj"); 
    ps.addBatch(); 
     
    ps.setInt(1, 62); 
    ps.setString(2, "haha"); 
    ps.setString(3, "bj"); 
    ps.addBatch(); 
     
    ps.setInt(1, 63); 
    ps.setString(2, "haha"); 
    ps.setString(3, "bj"); 
    ps.addBatch(); 
     
    ps.executeBatch(); 
    ps.close(); 
     
    conn.close(); 
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 
 }
 
 //将整批分批执行批处理
 //这才是理想的解决方案,它避免了SQL注入和内存不足的问题。看看我们如何递增计数器计数,一旦BATCHSIZE 达到 1000,我们调用executeBatch
 public void testBatchs(){
  String sql = "insert into employee (name, city, phone) values (?, ?, ?)";
  PreparedStatement ps = conn.prepareStatement(sql);
  final int batchSize = 1000;
  int count = 0;
  for (Employee employee: employees) {
      ps.setString(1, employee.getName());
      ps.setString(2, employee.getCity());
      ps.setString(3, employee.getPhone());
      ps.addBatch();
      if(++count % batchSize == 0) {
          ps.executeBatch();
      }
  }
  ps.executeBatch(); // insert remaining records
  ps.close();
  conn.close();

 }
 //执行事务
 public void testTransaction(){
   Statement stmt = null;
   try {
   conn.setAutoCommit(false);
   stmt = conn.createStatement(); 
   stmt.addBatch("insert into dept2 values (51, '500', 'haha')"); 
   stmt.addBatch("insert into dept2 values (52, '500', 'haha')"); 
   stmt.addBatch("insert into dept2 values (53, '500', 'haha')"); 
   stmt.executeBatch(); 
   conn.commit(); 
  } catch (SQLException e) {
   try {
    conn.rollback();
    conn.setAutoCommit(true);
   } catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   } 
   e.printStackTrace();
  }finally{
   try { 
      if(stmt != null) 
          stmt.close(); 
      if(conn != null) 
          conn.close(); 
    } catch (SQLException e) { 
        e.printStackTrace(); 
    } 
  }
 }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值