6-JDBC批处理和事务

JDBC批处理

        批量处理允许将相关的SQL语句分组到批处理中,并通过对数据库的一次调用提交它们。

        当需要一次向数据库发送多个SQL语句时,可以减少连接数据库的开销,从而提高性能。

  • Statement批处理:

        1 、注册驱动获取连接

        2、使用createStatement()方法创建Statement对象。

        3 、使用setAutoCommit()将auto-commit设置为false 。(可选)

        4 、使用addBatch()方法在创建的语句对象上添加您喜欢的SQL语句到批处理中。

        5 、在创建的语句对象上使用executeBatch()方法执行所有SQL语句。

        6 、使用commit()方法提交所有更改。(可选)

        7 、释放资源

// Create statement object
Statement stmt = conn.createStatement();

// Set auto-commit to false
conn.setAutoCommit(false);

// Create SQL statement
String SQL = "INSERT INTO Employees (id, first, last, age) " +
             "VALUES(200,'Zia', 'Ali', 30)";
// Add above SQL statement in the batch.
stmt.addBatch(SQL);

// Create one more SQL statement
String SQL = "INSERT INTO Employees (id, first, last, age) " +
             "VALUES(201,'Raj', 'Kumar', 35)";
// Add above SQL statement in the batch.
stmt.addBatch(SQL);

// Create one more SQL statement
String SQL = "UPDATE Employees SET age = 35 " +
             "WHERE id = 100";
// Add above SQL statement in the batch.
stmt.addBatch(SQL);

// Create an int[] to hold returned values
int[] count = stmt.executeBatch();

//Explicitly commit statements to apply changes
conn.commit();
public static void main(String[] args) throws Exception{
        Connection conn = DbUtils.getConnection();
        Statement stat = conn.createStatement();
        for (int i = 1; i < 10003; i++) {//百万级数据时,MySQL就会变得很慢了
            String sid = "s_600"+Integer.toString(i);
            stat.addBatch("insert into student value ('"+sid+"','张三','23')");
            if(i%1000 == 0){
                int[] results = stat.executeBatch();
                System.out.println("执行sql个数"+results.length);
                stat.clearBatch();
            }
        }
        System.out.println("----------------执行剩余的----------------");
        int[] results = stat.executeBatch();
        System.out.println("执行SQL个数"+results.length);
        System.out.println("执行完毕");

        DbUtils.closeAll(conn, stat, null);
    }
//改
stat.addBatch("update student set name='李四' where id = 's_6001000'");
//删
stat.addBatch("delete from student where id='s_600101'");
  • PrepareStatement批处理:

        1、使用占位符创建SQL语句。

        2、使用prepareStatement() 方法创建PrepareStatement对象。

        3、使用setAutoCommit()将auto-commit设置为false 。(可选)

        4、使用addBatch()方法在创建的语句对象上添加您喜欢的SQL语句到批处理中。

        5、在创建的语句对象上使用executeBatch()方法执行所有SQL语句。

        6、最后,使用commit()方法提交所有更改。(可选)

// Create SQL statement
String SQL = "INSERT INTO Employees (id, first, last, age) " +
             "VALUES(?, ?, ?, ?)";

// Create PrepareStatement object
PreparedStatemen pstmt = conn.prepareStatement(SQL);

//Set auto-commit to false
conn.setAutoCommit(false);

// Set the variables
pstmt.setInt( 1, 400 );
pstmt.setString( 2, "Pappu" );
pstmt.setString( 3, "Singh" );
pstmt.setInt( 4, 33 );
// Add it to the batch
pstmt.addBatch();

// Set the variables
pstmt.setInt( 1, 401 );
pstmt.setString( 2, "Pawan" );
pstmt.setString( 3, "Singh" );
pstmt.setInt( 4, 31 );
// Add it to the batch
pstmt.addBatch();

//add more batches

//Create an int[] to hold returned values
int[] count = stmt.executeBatch();

//Explicitly commit statements to apply changes
conn.commit();
    public static void main(String[] args) throws Exception{
        Connection conn = DbUtils.getConnection();
        PreparedStatement pstat = conn.prepareStatement("insert into student value (?,?,?)");
        for (int i = 1; i < 1003; i++) {//百万级数据时,MySQL就会变得很慢了
            String sid = "s_600"+Integer.toString(i);
            pstat.setString(1, sid);
            pstat.setString(2, "王五");
            pstat.setString(3, "xnsoqpnm");
            pstat.addBatch();
            if(i%100 == 0){
                int[] results = pstat.executeBatch();
                System.out.println("执行sql个数"+results.length);
                pstat.clearBatch();
            }
        }
        System.out.println("----------------执行剩余的----------------");
        int[] results = pstat.executeBatch();
        System.out.println("执行SQL个数"+results.length);
        System.out.println("执行完毕");

        DbUtils.closeAll(conn, pstat, null);
    }
  • Statment批处理和PrepareStatment批处理区别:

        (1)Statment批处理可以添加不同Sql语句,而PrepareStatment只能添加一类sql语句

        (2)PrepareStatment效率比Statment高,而且更安全。


JDBC操作二进制

        PreparedStatement对象可以使用输入和输出流来提供参数数据。这使您可以将整个文件放入可以保存大值的数据库列,例如Text和BLOB数据类型。

        有以下方法可用于流式传输数据:

                setAsciiStream():此方法用于提供大的ASCII值。

                setCharacterStream():此方法用于提供大型UNICODE值。

                setBinaryStream():此方法用于提供较大的二进制值。

        setXXXStream():方法除了参数占位符之外还需要额外的参数,文件大小。

<!-- XML文件 -->
<?xml version="1.0" encoding="UTF-8"?>
<Employee>
  <id>100</id>
  <first>张</first>
  <last>无极</last>
  <Salary>10000</Salary>
  <Dob>18-08-1978</Dob>
</Employee>
  • 文本图片存取案例:
    public static void main(String[] args) throws Exception{
//        setBigData();
        Connection conn = DbUtils.getConnection();
        PreparedStatement pstat = conn.prepareStatement("select * from bigdata where id=?");
        pstat.setInt(1, 3);
        ResultSet rs = pstat.executeQuery();
        if(rs.next()){
            System.out.println("----------------处理文本----------------");
            Reader reader = rs.getCharacterStream("content");
            BufferedReader br = new BufferedReader(reader);
            String data = null;
            while ((data=br.readLine()) != null){
                System.out.println(data);
            }
            System.out.println("----------------处理图片----------------");
            InputStream is = rs.getBinaryStream("photo");
            FileOutputStream fos = new FileOutputStream("D:\\我的图片\\logo111.png");
            int d=0;
            while ((d=is.read()) != -1){
                fos.write(d);
            }
            fos.close();
            is.close();
        }
    }

    public static void setBigData() throws SQLException, FileNotFoundException {
        Connection conn = DbUtils.getConnection();
        PreparedStatement pstat = conn.prepareStatement("insert into bigdata(content,photo) value (?,?)");
        File file1 = new File("src\\emp.xml");
        File file2 = new File("D:\\我的图片\\logo.png");
        FileInputStream fis1 = new FileInputStream(file1);
        FileInputStream fis2 = new FileInputStream(file2);
        pstat.setAsciiStream(1, fis1,file1.length());
        pstat.setBinaryStream(2, fis2,file2.length());
        pstat.executeUpdate();
        DbUtils.closeAll(conn, pstat, null);
        System.out.println("执行完毕");
    }

        运行结果:

                        

  • 关闭资源案例:
import java.sql.*;
import java.io.*;
import java.util.*;

public class JDBCExample {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost:3306/EMP";

   //  Database credentials
   static final String USER = "username";
   static final String PASS = "password";
   
   public static void main(String[] args) {
   Connection conn = null;
   PreparedStatement pstmt = null;
   Statement stmt = null;
   ResultSet rs = null;
   try{
      // Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");
      // Open a connection
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);

      //Create a Statement object and build table
      stmt = conn.createStatement();
      createXMLTable(stmt);

      //Open a FileInputStream
      File f = new File("XML_Data.xml");
      long fileLength = f.length();
      FileInputStream fis = new FileInputStream(f);

      //Create PreparedStatement and stream data
      String SQL = "INSERT INTO XML_Data VALUES (?,?)";
      pstmt = conn.prepareStatement(SQL);
      pstmt.setInt(1,100);
      pstmt.setAsciiStream(2,fis,(int)fileLength);
      pstmt.execute();

      //Close input stream
      fis.close();

      // Do a query to get the row
      SQL = "SELECT Data FROM XML_Data WHERE id=100";
      rs = stmt.executeQuery (SQL);
      // Get the first row
      if (rs.next ()){
         //Retrieve data from input stream
         InputStream xmlInputStream = rs.getAsciiStream (1);
         int c;
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         while (( c = xmlInputStream.read ()) != -1)
            bos.write(c);
         //Print results
         System.out.println(bos.toString());
      }
      // Clean-up environment
      rs.close();
      stmt.close();
      pstmt.close();
      conn.close();
   }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
      try{
         if(stmt!=null)
            stmt.close();
      }catch(SQLException se2){
      }// nothing we can do
      try{
         if(pstmt!=null)
            pstmt.close();
      }catch(SQLException se2){
      }// nothing we can do
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   }//end try
   System.out.println("Goodbye!");
}//end main

public static void createXMLTable(Statement stmt) 
   throws SQLException{
   System.out.println("Creating XML_Data table..." );
   //Create SQL Statement
   String streamingDataSql = "CREATE TABLE XML_Data " +
                             "(id INTEGER, Data TEXT)";
   //Drop table first if it exists.
   try{
      stmt.executeUpdate("DROP TABLE IF EXISTS XML_Data");
      //Build table.
   	  stmt.executeUpdate(streamingDataSql);
   }catch(SQLException se){
   }// do nothing
  
}//end createXMLTable
}//end JDBCExample

JDBC中事务应用

数据库事务_头秃人强的博客-优快云博客

        JDBC连接默认处于自动提交模式,则每个SQL语句在完成后都会提交到数据库。

        事务使您能够控制何时将更改应用于数据库。它将单个SQL语句或一组SQL语句视为一个逻辑单元,如果任何语句失败,则整个事务将失败。

//启用手动事务支持,而不是JDBC驱动程序默认使用的自动提交模式,可以传递一个布尔值true来重新打开它
conn.setAutoCommit(false);
//提交更改:在连接对象上调用commit()方法
conn.commit();
//使用连接名为conn的数据库回滚更新
conn.rollback();

//设置事务隔离级别(例:脏读)
conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
try{
   //开启事务
   conn.setAutoCommit(false);
   Statement stmt = conn.createStatement();
   
   String SQL = "INSERT INTO Employees  " +
                "VALUES (106, 20, 'Rita', 'Tez')";
   stmt.executeUpdate(SQL);  
   //有可能出现异常
   String SQL = "INSERT IN Employees  " +
                "VALUES (107, 22, 'Sita', 'Singh')";
   stmt.executeUpdate(SQL);
   // 没有错误提交
   conn.commit();
}catch(SQLException se){
   //出现错误回滚
   conn.rollback();
   conn.commit();
}
  •  Savepoint :

        新的JDBC 3.0 Savepoint接口提供了额外的事务控制。

        设置保存点时,可以在事务中定义逻辑回滚点。如果通过保存点发生错误,则可以使用回滚方法来撤消所有更改,或仅撤销在保存点之后所做的更改。

//Connection对象有两种新的方法来帮助您管理保存点 :

//定义新的保存点。它还返回一个Savepoint对象
setSavepoint(String savepointName)

//删除保存点
//注意,它需要一个Savepoint对象作为参数。此对象通常是由setSavepoint()方法生成的保存点
releaseSavepoint(Savepoint savepointName)
    public static void main(String[] args){
        Connection conn =null;
        Statement stat = null;
        Savepoint savepoint1 = null;
        Savepoint savepoint2 = null;

        try {
            conn = DbUtils.getConnection();
            //开启事务
            conn.setAutoCommit(false);

            stat = conn.createStatement();
            //转出
            stat.executeUpdate("update account set money = money-1000 where id=1;");
            //回滚点1
            savepoint1 = conn.setSavepoint("Savepoint1");

            //模拟断电
//            int i = 10/0;

            //转入
            stat.executeUpdate("update account set money = money+1000 where id=2;");
            //回滚点2
            savepoint2 = conn.setSavepoint("savepoint2");

            //添加账户
            stat.executeUpdate("insert into account value (3,'王五',5000);");
            //提交
            conn.commit();
            System.out.println("执行成功");
        } catch (Exception e) {
            e.getMessage();
            try {
                conn.rollback(savepoint1);
//                conn.rollback(savepoint2);
                conn.commit();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            System.out.println("回滚");
        }finally {
            DbUtils.closeAll(conn, stat, null);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值