其他分享
首页 > 其他分享> > 利用PreparedStatement操作Blob和Text数据

利用PreparedStatement操作Blob和Text数据

作者:互联网

BLOB类型字段

TEXT字段L

向数据表中插入BLOB数据和TEXT类型数据

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, "承夕", "19834666@qq.com", "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 ;
    }
}

注意点:

笔者在进行数据插入text类型的时候,使用FileInputStream的txt文件在插入数据库时出现了乱码现象,然后改成使用FileInputReader,但是这是的SetObject方法却也出现错误,无奈之下只能使用文档的setCharacter方法插入text.txt文件,请问有声明办法解决这个问题呢。

修改数据表中的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;
    }
}

从数据表中读取大数据类型

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 · 访问量 425 私信 关注

标签:PreparedStatement,Text,args,Blob,conn,sql,import,com,pstm
来源: https://blog.csdn.net/weixin_45062761/article/details/104587210