テキストデータのPreparedStatementを使用してBLOBおよび操作

BLOB型のフィールド

  • BLOBは、BLOBデータ型は文字列がスプライシングさすることができないので、PreparedStatementのは、使用しなければならないコンテナは、データBLOB型を挿入し、データの異なるサイズに対応することができ、大量のデータを格納することができる、バイナリラージオブジェクトであります
  • 255 BLOB型65000 MEDIUMBLOBタイプタイプ最大最大最大タイプ4G 16MバイトLONGBLOB 4種類、TINYBLOBの最大値があります。
  • ただし、ファイルがパフォーマンスにあまり影響を格納されている場合

テキストフィールドL

  • 4種類、256バイトのTINYTEXT最大、TEXTタイプの64キロバイト、MEDIUMTEXT最大16M、LONGTEXTタイプ最大4Gがあります。

データテーブルにTEXTタイプBLOBデータの挿入とデータ

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 ;
    }
}

注意点:

著者は、データベース文字化け現象に挿入されたときにファイルのTXTのFileInputStreamを使用すると、表示されたときに、テキスト型にデータを実施し、その後FileInputReaderを使用するように変更しましたが、これは、彼らがそこにエラーのsetObjectメソッドで、絶望しかsetCharacter文書を使用することができますメソッド挿入TEXT.TXTファイル、この問題文を解決する方法があります。

テーブルブロブのフィールドのデータを変更します

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;
    }
}

大規模なデータ型は、データテーブルから読み取ります

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 ;
    }
}

 

 

 

公開された34元の記事 ウォンの賞賛4 ビュー426

おすすめ

転載: blog.csdn.net/weixin_45062761/article/details/104587210