Handle BLOB

Original link: http://www.cnblogs.com/yangHS/p/10832890.html

Handle BLOB

oracle LOB

LOB (Large Objects large objects), for storing a data type of a large number of binary and text data

Internal LOB

    BLOB (binary data)

    The CLOB (single-byte alphanumeric)

    NCLOB (multi-byte character data)

External LOB

Inserts BLOB data types must be used PreparedStatement.

Because they can not string spelling when BLOB data types.

 

Call setBlob (int index, InputStream inputStream)

 @Test
    public void test4(){
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "insert into student values(?,?,?,?)";

        try {
            conn = Methods.getConnection();

          
            preparedStatement = conn.prepareStatement(sql);

            preparedStatement.setObject(1,"5");
            preparedStatement.setString(2,"yang5");
            preparedStatement.setString(3,"1235");
            InputStream inputStream = new FileInputStream("boy.png");
            preparedStatement.setBlob(4,inputStream);
            preparedStatement.executeUpdate();
        inputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { Methods.release(preparedStatement,conn,resultSet); } }

  

 

Read blob data:

1. The method of reading a Blob object getBlob

2. Call Blob of the getBinaryStream () method to get the input stream, to operate in the IO.

@Test
    public void test5(){
        Connection conn = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select picture from student where id =5";

        try {
            conn = Methods.getConnection();


            preparedStatement = conn.prepareStatement(sql);

            resultSet = preparedStatement.executeQuery();

            if(resultSet.next()){
                Blob picture = resultSet.getBlob(1);
                InputStream in = picture.getBinaryStream();
                OutputStream os = new FileOutputStream("boy1.png");
                byte[] buffer = new byte[1024];
                int len = 0;
                while((len= in.read(buffer))!=-1){
                    os.write(buffer,0,len);
                }
                os.close();
                in.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Methods.release(preparedStatement,conn,resultSet);
        }
    }

  

Reproduced in: https: //www.cnblogs.com/yangHS/p/10832890.html

Guess you like

Origin blog.csdn.net/weixin_30810583/article/details/94786058
Recommended