ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

利用PreparedStatement操作Blob和Text数据

2020-03-01 14:00:53  阅读:471  来源: 互联网

标签:PreparedStatement Text args Blob conn sql import com pstm


BLOB类型字段

  • BLOB是一个二进制大型对象,是一个可以存储大量数据的容器,它能容纳不同大小的数据,插入blob类型的数据,必须使用PreparedStatement,因为BLOB类型数据是不能通过字符串拼接的
  • 有四种类型,TinyBlob类型最大字节255   Blob类型最大65k  MediumBlob类型最大16M  LongBlob类型最大4G
  • 但是如果存储的文件过大会影响到性能

TEXT字段L

  • 也有四种类型,TinyText 最大256字节,TEXT类型64kb ,MediumTEXT最大16M,LongTEXT类型最大4G

向数据表中插入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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有