Blob and operation using the Text data PreparedStatement

BLOB type field

  • BLOB is a binary large object, a container can store large amounts of data, it can accommodate different sizes of data, inserting data blob types, the PreparedStatement must be used, because the BLOB data type is a string can not be spliced
  • There are four types, TinyBlob maximum byte 255 Blob type 65k MediumBlob type Type Maximum Maximum Maximum Type 4G 16M LongBlob
  • However, if the file is stored too much effect on the performance

TEXT field L

  • There are four types, TinyText maximum of 256 bytes, TEXT types 64kb, MediumTEXT maximum 16M, LongTEXT Type Max 4G

TEXT types BLOB data insertion and data to a data table

package com.blobANDtext;

import com.utils.JDBCUtils;
import com.utils.Read;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author 承夕
 * @date 2020/3/1 0001 - 10:03
 * @contact:https://github.com/chengxi0
 */
public class BlobAndTextDemo1
{
    public static void main(String[] args) throws IOException {
        String sql = "insert into customers (name ,email , birth ,photo,`text`) values(?,?,?,?,?) ;";
        FileInputStream fi = new FileInputStream(new File("photos/proxy.jpg"));
        InputStreamReader fi2 = new InputStreamReader(new FileInputStream("texts/myself.txt"));
        int i = insertBlobText(sql, "承夕", "[email protected]", "1999-05-17", fi,fi2);

        System.out.println(i);

        fi2.close();
        fi.close();
    }

    public static int insertBlobText(String sql, Object... args) {
        Connection conn = null ;
        PreparedStatement pstm = null ;

        try {
            //获取连接
            conn = JDBCUtils.getConnection();

            //获取PreparedStatement
            pstm = conn.prepareStatement(sql);

            //填充占位符
            for (int i = 0; i < args.length; i++) {
                if (args[i].getClass() == InputStreamReader.class) {
                    pstm.setCharacterStream(i + 1, (InputStreamReader)args[i]);
                }else {
                    pstm.setObject(i + 1, args[i]);
                }
            }

            int i = pstm.executeUpdate();

            return i ;

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, pstm, null);
        }
        return  0 ;
    }
}

important point:

The author performed data into text type when using FileInputStream txt file appears when inserted into the database garbled phenomenon, then changed to use FileInputReader, but this is the method they SetObject error there, desperation can only use setCharacter document method insertion text.txt file, is there a way to solve this problem statement.

Modify the field data in the table Blob

package com.blobANDtext;

import com.utils.JDBCUtils;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author 承夕
 * @date 2020/3/1 0001 - 12:53
 * @contact:https://github.com/chengxi0
 */
public class BlobAndTextDemo2 {
    public static void main(String[] args) throws IOException {
        String sql = "update customers set photo = ? where id = ?";
        FileInputStream is = new FileInputStream(new File("photos/plane.jpg"));
        updateBlobText(sql,is,21);
        is.close();
    }

    public static int updateBlobText(String sql, Object... args) {
        Connection conn = null ;
        PreparedStatement pstm = null ;
        try {
            //获取连接
            conn = JDBCUtils.getConnection();
            //获取PreparedStatement对象
            pstm = conn.prepareStatement(sql);

            //填充占位符
            for (int i = 0; i < args.length; i++) {
                pstm.setObject(i + 1, args[i]);
            }

            int i = pstm.executeUpdate();

            return i;

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn, pstm, null);
        }
        return  0;
    }
}

Large data type read from the data table

package com.blobANDtext;

import com.utils.JDBCUtils;

import java.io.*;
import java.sql.*;

/**
 * @author 承夕
 * @date 2020/3/1 0001 - 13:18
 * @contact:https://github.com/chengxi0
 */
public class BlobAndTextDemo3 {
    public static void main(String[] args) {
        String sql = "select photo from customers where id = ?";
        int i = readBlobText(sql, 21);
        System.out.println(i);

    }

    public static int readBlobText(String sql ,Object...args) {
        Connection conn = null ;
        PreparedStatement pstm = null ;
        try {
            //获取连接
            conn = JDBCUtils.getConnection();
            //获取PreparedStatement对象
            pstm = conn.prepareStatement(sql);

            //填充占位符
            for (int i = 0; i < args.length; i++) {
                pstm.setObject(i + 1, args[i]);
            }

            ResultSet rs = pstm.executeQuery();
            if(rs.next()){
                InputStream is = rs.getBinaryStream(1);
                //接下就是输入输出流的对接
                FileOutputStream fos = new FileOutputStream(new File("downloadPhotos/plane.jpg"));
                int len = 0 ;
                byte[] brr = new byte[1024*8];
                while ((len = is.read(brr) )!= -1) {
                    fos.write(brr, 0, len);
                }
                fos.close();
                is.close();
            }
            return 1;

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, pstm, null);
        }
        return  0 ;
    }
}

 

 

 

Published 34 original articles · won praise 4 · Views 426

Guess you like

Origin blog.csdn.net/weixin_45062761/article/details/104587210