使用JDBC处理MySQL大文本和大数据

使用JDBC处理MySQL大文本和大数据

概述

LOB,Large Objects,是一种用于存储大对象的数据类型,一般LOB又分为BLOB与CLOB。BLOB通常用于存储二进制数据,比如图片、音频、视频等。CLOB通常用于存储大文本,比如小说。

MySQL数据库中没有专门的CLOB数据类型,而如果要存储大文本,MySQL采用的是TEXT类型。TEXT类型又有TINYTEXT、TEXT、MEDIUMTEXT和LONGTEXT之分。MySQL中的BLOB类型又可分为TINYBLOB、BLOB、MEDIUMBLOB和LONGBLOB。

使用JDBC处理大文本

向MySQL中存储大文本,可调用JDBC API中PreparedStatement的如下方法:

// 将指定参数设置为给定 Reader 对象
void setCharacterStream(int parameterIndex, Reader reader) throws SQLException 
// 将给定参数设置为给定 Reader 对象,该对象具有给定字符数长度(int型)
void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException 
// 将指定参数设置为给定 Reader 对象,该对象具有给定字符数长度(long型)
void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException 

如果需要从MySQL数据库中获取大文本列字段值,则可以使用ResultSet的如下方法:

// 以 java.io.Reader 对象的形式获取此 ResultSet 对象的当前行中指定列的值
Reader getCharacterStream(int columnIndex) throws SQLException
Reader getCharacterStream(String columnLabel) throws SQLException

// 以 String 的形式获取此 ResultSet 对象的当前行中指定列的值
String getString(int columnIndex) throws SQLException
String getString(String columnLabel) throws SQLException

示例

表:

小说表

代码:

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

/**
 * 使用JDBC操作大文本
 * 
 * @author Administrator
 *
 */
public class ClobTest {

    /**
     * 使用JDBC向数据库表中插入大文本数据
     * 
     * @throws SQLException
     */
    public static void add() throws SQLException {
        Connection conn = null;
        PreparedStatement pstmt = null;
        // 获取数据库会话对象
        JdbcSession session = JdbcSessionFactory.getCurrentSession();
        // 获取数据库连接
        conn = session.getConnection();
        // 创建SQL语句:向小说表中添加一条章节内容的记录
        String sql = "INSERT INTO novel(content) VALUES(?)";
        // 创建PreparedStatement对象
        pstmt = conn.prepareStatement(sql);
        // 创建Reader对象
        File file = new File(Thread.currentThread().getClass()
                .getResource("/novel/1.txt").getPath());
        Reader reader = null;
        try {
            reader = new FileReader(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 设置参数
        pstmt.setCharacterStream(1, reader, (int) file.length());
        // 执行SQL语句
        int count = pstmt.executeUpdate();
        // 处理结果
        if (count > 0)
            System.out.println("添加成功");
        else
            System.out.println("添加失败");
        // 释放资源
        JdbcResourceManager.close(pstmt);
        JdbcResourceManager.close(conn);
        JdbcSessionFactory.closeSession();
    }

    public static void read() throws SQLException {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        // 获取数据库会话对象
        JdbcSession session = JdbcSessionFactory.getCurrentSession();
        // 获取数据库连接
        conn = session.getConnection();
        // 创建SQL语句
        String sql = "SELECT id, content FROM novel";
        // 创建PreparedStatement对象
        pstmt = conn.prepareStatement(sql);
        // 执行SQL语句
        rs = pstmt.executeQuery();
        // 处理结果
        while (rs.next()) {
            // 读取小说内容
            Reader reader = rs.getCharacterStream("content");
            int ch;
            try {
                while((ch = reader.read()) != -1){
                    System.out.print((char)ch);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 释放资源
        JdbcResourceManager.close(rs);
        JdbcResourceManager.close(pstmt);
        JdbcResourceManager.close(conn);
        JdbcSessionFactory.closeSession();
    }

    public static void main(String[] args) {
        try {
            add();
            read();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

使用JDBC处理二进制数据

向MySQL中存储二进制数据,可调用JDBC API中PreparedStatement的如下方法:

// 将指定参数设置为给定输入流。 
void setBinaryStream(int parameterIndex, InputStream x) 
// 将指定参数设置为给定输入流,该输入流将具有给定字节数(int型)。 
void setBinaryStream(int parameterIndex, InputStream x, int length) 
// 将指定参数设置为给定输入流,该输入流将具有指定字节数(long型)。 
void setBinaryStream(int parameterIndex, InputStream x, long length) 

如果需要从MySQL数据库中获取二进制列字段值,则可以使用ResultSet的如下方法:

// 以未解释字节的流的形式获取此 ResultSet 对象的当前行中指定列的值。
InputStream getBinaryStream(int columnIndex) 
// 以未解释的 byte 流的形式获取此 ResultSet 对象的当前行中指定列的值。
InputStream getBinaryStream(String columnLabel) 

// 以 Java 编程语言中 Blob 对象的形式获取此 ResultSet 对象的当前行中指定列的值。 
Blob getBlob(int columnIndex)
// 以 Java 编程语言中 Blob 对象的形式获取此 ResultSet 对象的当前行中指定列的值。
Blob getBlob(String columnLabel)

获取Blob对象后可以继续调用getBinaryStream()方法获取输入流。

示例

表:

音乐表

代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 使用JDBC操作二进制数据
 * 
 * @author 小明
 *
 */
public class BlobTest {

    /**
     * 插入二进制数据到数据库
     * 
     * @throws Exception
     */
    public static void add() throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        // 获取数据库会话对象
        JdbcSession session = JdbcSessionFactory.getCurrentSession();
        // 获取数据库连接
        conn = session.getConnection();
        // 插入音乐数据的SQL语句
        String sql = "INSERT INTO music(content) VALUES(?)";
        pstmt = conn.prepareStatement(sql);
        // 创建PreparedStatement对象
        pstmt = conn.prepareStatement(sql);
        // 创建Reader对象
        File file = new File(Thread.currentThread().getClass()
                .getResource("/music/08.Along_in_the_night.mp3").getPath());
        InputStream fis = new FileInputStream(file);// 生成的流
        pstmt.setBinaryStream(1, fis, file.length());
        // 执行SQL语句
        int count = pstmt.executeUpdate();
        // 处理结果
        if (count > 0)
            System.out.println("添加成功");
        else
            System.out.println("添加失败");
        // 释放资源
        JdbcResourceManager.close(pstmt);
        JdbcResourceManager.close(conn);
        JdbcSessionFactory.closeSession();
    }

    /**
     * 从数据库中读二进制数据
     * 
     * @throws Exception
     */
    public static void read() throws Exception {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        // 获取数据库会话对象
        JdbcSession session = JdbcSessionFactory.getCurrentSession();
        // 获取数据库连接
        conn = session.getConnection();
        // 创建SQL语句
        String sql = "SELECT id, content FROM music WHERE id=?";
        // 创建PreparedStatement对象
        pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, 1);
        // 执行SQL语句
        rs = pstmt.executeQuery();
        // 处理结果
        if (rs.next()) {
            InputStream in = rs.getBinaryStream("content"); // 获取列字段InputStream对象
            // 缓冲数组
            byte buf[] = new byte[1024];
            int len;
            // 输出流,将读取到的音乐数据保存到D盘
            OutputStream out = new FileOutputStream("D:\\1.mp3");
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        }
        // 释放资源
        JdbcResourceManager.close(rs);
        JdbcResourceManager.close(pstmt);
        JdbcResourceManager.close(conn);
        JdbcSessionFactory.closeSession();
    }

    public static void main(String[] args) throws Exception {
        add();
        read();
    }
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值