JDBC_2Blob data type and batch operation

JDBC Blob data type


PreparedStatement operation Blob type data

Blob is a binary large object

Statement cannot manipulate Blob data type, thinking that Blob data type cannot be spliced ​​with string, PreparedStatement can manipulate Blob data type to
insert Blob


        //插入Blob
        String sql = "insert into customers(name,email,birth,photo) values(?,?,?,?)";
        java.sql.PreparedStatement ps = connection.prepareStatement(sql);
        ps.setObject(1,"张宇豪");
        ps.setObject(2,"[email protected]");
        ps.setObject(3,"1992-09-08");
        FileInputStream fileInputStream = new FileInputStream(new File("play.jpg"));
        ps.setBlob(4,fileInputStream);
        ps.execute();
        ps.close();
        connection.close();

Query Blob

//查询Blob
        String sql = "select id,name,birth,photo from customers where id = ?";
        java.sql.PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,2);
        ResultSet resultSet = ps.executeQuery();
        if(resultSet.next()){
    
    
            int id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            String email = resultSet.getString(3);
            Date birth = resultSet.getDate(4);
            Blob photo = resultSet.getBlob(5);

            //将Blob类型的字段下载下来,以文件的方式保存在本地
            InputStream is = photo.getBinaryStream();
            FileOutputStream fos = new FileOutputStream("zhangyuhao.jpg");
            byte[] buffer = new byte[1024];
            int len;
            while ((len = is.read(buffer)) != -1){
    
    
                fos.write(buffer,0,len);
            }
        }

        ps.execute();
        ps.close();
        connection.close();

Batch operations (mainly inserts)
PreparedStatement is more efficient than Statement
Reason: DBServer will provide performance optimization for prepared statements. Because the prepared statement may be called repeatedly, the execution code of the statement after being compiled by DBServer's compiler is cached, so as long as the same prepared statement is called next time, no compilation is required, as long as the parameters are directly passed in The compiled statement will be executed in the execution code, and the database will not cache the Statement statement

Further optimization:
addBatch() method executeBatch() clearBatch() (similar to the principle of caching)
Note: By default, the mysql server closes batch processing by default. We need to pass a parameter to enable mysql to enable batch processing support.

//批量插入
        String sql = "insert into goods(name) values(?)";
        java.sql.PreparedStatement ps = connection.prepareStatement(sql);
        for(int i = 1;i <= 20000;i ++){
    
    
            ps.setObject(1,"name_" + i);
            ps.addBatch();
            
            if(i % 500 == 0){
    
    
                ps.execute();
                ps.clearBatch();
            }
            
        }

Further optimization
Set connection.setAutoCommit(false), and finally wait for all sql statements to execute unified commit (equivalent to all statements are cached).

Guess you like

Origin blog.csdn.net/m0_46656833/article/details/112494967