clob-数据库存取大文本文件、二进制图片

 将文件中所有数据(即大文本)作为数据库表某一列值存入:

代码涉及到IOSQL的相关包:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

 数据库存储大文本对象(clob-来自文件) 


    /**
     * 读文件并保存到数据库(reader输入流:文件——数据库)
     */
    static void read() {
        String url = "jdbc:mysql://192.168.1.45:3306/employ";
        String uerName = "root";
        String password = "udd@123";
        try {// 加载mysql数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        /**
         * 要读的大文本
         **/
        File file = new File("E:\\data\\logs\\udd-config-admin.log");
        if (!file.exists()) {
            return;
        }
        FileReader reader = null;// 输入流reader
        try {
            reader = new FileReader(file);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {// 获取数据库连接对象
            conn = DriverManager.getConnection(url, uerName, password);
            String sql = "insert into  clob(clob_col) values(?)";
            // 获取预处理对象(标准sql存储起来)
            pstmt = conn.prepareStatement(sql);
            pstmt.setCharacterStream(1, reader, file.length());// file大文本内容读到数据库
            pstmt.executeUpdate();// 执行数据库更新
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                    pstmt = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

  

将数据库中大文本字段读出并写入文件方法:

    void writer() {
        String url = "jdbc:mysql://192.168.1.45:3306/employ";
        String uerName = "root";
        String password = "udd@123";
        try {// 加载mysql数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        /**
         * 待写入数据的文件
         **/
        File file = new File("D:\\data\\test.log");

        FileWriter writer = null;// 输出流writer
        try {
            if (!file.exists()) {// 文件不存在,创建空文件
                file.createNewFile();
            }
            writer = new FileWriter(file);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {// 获取数据库连接对象
            conn = DriverManager.getConnection(url, uerName, password);

            String sql = "select * from clob ";
            // 获取预处理对象(标准sql存储起来)
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();// 去数据库查询

            while (rs.next()) {
                // 从结果集中读出大文本数据
                Reader rd = rs.getCharacterStream(2);
                char[] buf = new char[1024];
                int len = rd.read(buf);
                while (len != -1) {
                    writer.write(buf, 0, len);
                    len = rd.read(buf);
                }
            }
            writer.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (pstmt != null) {
                    pstmt.close();
                    pstmt = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

代码中涉及到的表: 

 

CLOB和BLOB的区别

CLOB使用CHAR来保存数据。  如:保存XML文档、文件等

BLOB就是使用二进制保存数据。  如:保存位图、图片、视频等

用blob存取图片安全性更高

因blob与clob数据存取操作类似,下面仅仅贴出blob相关主要代码及数据库样式:

      /**
         * 要保存的图片文件
         **/
        File file = new File("E:\\flower.jpg");
        if (!file.exists()) {
            return;
        }
        FileInputStream fis = null;// 输入流
        BufferedInputStream bis = null;// 过滤流,加速

        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {// 获取数据库连接对象
            conn = DriverManager.getConnection(url, uerName, password);
            String sql = "insert into  clob(blob_col) values(?)";
            // 获取预处理对象(标准sql存储起来)
            pstmt = conn.prepareStatement(sql);
            pstmt.setBinaryStream(1, bis, file.length());// 图片对应二进制读到数据库
            pstmt.executeUpdate();// 执行数据库更新
        } catch (SQLException e) {
            e.printStackTrace();
        } 

读出数据库中二进制图片:

    /**
         * 图片显示位置
         **/
        File file = new File("D:\\data\\tt.jpg");

        FileOutputStream fos = null;// 输出流
        BufferedOutputStream bos = null;
        try {
            if (!file.exists()) {// 文件不存在,创建空文件
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {// 获取数据库连接对象
            conn = DriverManager.getConnection(url, uerName, password);

            String sql = "select * from clob ";
            // 获取预处理对象(标准sql存储起来)
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();// 去数据库查询

            while (rs.next()) {
                // 从结果集中读出大文本数据
                InputStream is = rs.getBinaryStream(2);
                byte[] buf = new byte[1024];
                int len = is.read(buf);
                while (len != -1) {
                    bos.write(buf, 0, len);
                    len = is.read(buf);
                }
            }
            bos.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

表:

猜你喜欢

转载自blog.csdn.net/qq_34777858/article/details/82802122