ClobTest BlobTest



package cn.itcast.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * 2008-12-6
 *
 * @author <a href="mailto:[email protected]">李勇</a>
 *
 */
public final class JdbcUtils {
    private static String url = "jdbc:mysql://localhost:3306/jdbc";
    private static String user = "root";
    private static String password = "";

    private JdbcUtils() {
    }

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    public static void free(ResultSet rs, Statement st, Connection conn) {
        try {
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (st != null)
                    st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (conn != null)
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
            }
        }
    }
}

package cn.itcast.jdbc;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * 2008-12-6
 *
 * @author <a href="mailto:[email protected]">liyong</a>
 *
 */
public class ClobTest {

    /**
     * @param args
     * @throws IOException
     * @throws SQLException
     */
    public static void main(String[] args) throws SQLException, IOException {
        // create();
        read();
    }

    static void read() throws SQLException, IOException {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            // 2.建立连接
            conn = JdbcUtils.getConnection();
            // conn = JdbcUtilsSing.getInstance().getConnection();
            // 3.创建语句
            st = conn.createStatement();

            // 4.执行语句
            rs = st.executeQuery("select big_text  from clob_test");

            // 5.处理结果
            while (rs.next()) {
                Clob clob = rs.getClob(1);
                Reader reader = clob.getCharacterStream();
                // reader = rs.getCharacterStream(1);
                // String s = rs.getString(1);

                File file = new File("JdbUtils_bak.java");
                Writer writer = new BufferedWriter(new FileWriter(file));
                char[] buff = new char[1024];
                for (int i = 0; (i = reader.read(buff)) > 0;) {
                    writer.write(buff, 0, i);
                }
                writer.close();
                reader.close();
            }
        } finally {
            JdbcUtils.free(rs, st, conn);
        }
    }

    static void create() throws SQLException, IOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 2.建立连接
            conn = JdbcUtils.getConnection();
            // conn = JdbcUtilsSing.getInstance().getConnection();
            // 3.创建语句
            String sql = "insert into clob_test(big_text) values (?) ";
            ps = conn.prepareStatement(sql);
            File file = new File("src/cn/itcast/jdbc/JdbcUtils.java");
            Reader reader = new BufferedReader(new FileReader(file));

            ps.setCharacterStream(1, reader, (int) file.length());
            // ps.setString(1, x);
            // 4.执行语句
            int i = ps.executeUpdate();

            reader.close();

            System.out.println("i=" + i);
        } finally {
            JdbcUtils.free(rs, ps, conn);
        }
    }

}
package cn.itcast.jdbc;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * 2008-12-6
 *
 * @author <a href="mailto:[email protected]">liyong</a>
 *
 */
public class BlobTest {

    /**
     * @param args
     * @throws IOException
     * @throws SQLException
     */
    public static void main(String[] args) throws SQLException, IOException {
        // create();
        read();
    }

    static void read() throws SQLException, IOException {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            // 2.建立连接
            conn = JdbcUtils.getConnection();
            // conn = JdbcUtilsSing.getInstance().getConnection();
            // 3.创建语句
            st = conn.createStatement();

            // 4.执行语句
            rs = st.executeQuery("select big_bit  from blob_test");

            // 5.处理结果
            while (rs.next()) {
                // Blob blob = rs.getBlob(1);
                // InputStream in = blob.getBinaryStream();
                InputStream in = rs.getBinaryStream("big_bit");

                File file = new File("IMG_0002_bak.jpg");
                OutputStream out = new BufferedOutputStream(
                        new FileOutputStream(file));
                byte[] buff = new byte[1024];
                for (int i = 0; (i = in.read(buff)) > 0;) {
                    out.write(buff, 0, i);
                }
                out.close();
                in.close();
            }
        } finally {
            JdbcUtils.free(rs, st, conn);
        }
    }

    static void create() throws SQLException, IOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            // 2.建立连接
            conn = JdbcUtils.getConnection();
            // conn = JdbcUtilsSing.getInstance().getConnection();
            // 3.创建语句
            String sql = "insert into blob_test(big_bit) values (?) ";
            ps = conn.prepareStatement(sql);
            File file = new File("IMG_0002.jpg");
            InputStream in = new BufferedInputStream(new FileInputStream(file));

            ps.setBinaryStream(1, in, (int) file.length());
            // 4.执行语句
            int i = ps.executeUpdate();

            in.close();

            System.out.println("i=" + i);
        } finally {
            JdbcUtils.free(rs, ps, conn);
        }
    }
}
时间: 2024-12-28 00:48:00

ClobTest BlobTest的相关文章

JDBC处理文本和二进制文件

JDBC支持文本(CLOB)和二进制(BLOB)文件的处理,比如要往数据库里存取文章或者图片.这都是用流的思想来解决的. 来两个Demo看看JDBC是怎么操作文本和二进制文件的. CLOB: package com.wxisme.jdbcclob; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java

使用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. 使用JDB

JDBC读写MySQL的大字段数据

JDBC读写MySQL的大字段数据 不管你是新手还是老手,大字段数据的操作常常令你感到很头痛.因为大字段有些特殊,不同数据库处理的方式不一样,大字段的操作常常是以流的方式 来处理的.而非一般的字段,一次即可读出数据.本人以前用到Spring+iBatis架构来操作大字段,结果以惨烈失败而告终,在网上寻求解决方案,也 没找到答案.最终以JDBC来实现了大字段操作部分. 本文以MySQL为例,通过最基本的JDBC技术来处理大字段的插入.读取操作. 环境: MySQL5.1 JDK1.5 一.认识My

JavaWeb-15 (JDBC编程)

JavaWeb-15:JDBC编程 JDBC编程 一.JDBC简介 数据库驱动:SUN公司为了简化.统一对数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC. JDBC全称为:Java Data Base Connectivity(java数据库连接),它主要由接口组成. 组成JDBC的2个包: java.sql javax.sql 开发JDBC应用需要以上2个包的支持外,还需要导入相应JDBC的数据库实现(即数据库驱动). 二.第一个JDBC程序 演示JDBC的项目: 1.把数据

mysql存取blob类型数据

参考网址:http://www.cnblogs.com/jway1101/p/5815658.html 首先是建表语句,需要实现将表建立好. CREATE TABLE `blobtest` ( `primary_id` varchar(32) NOT NULL, `bank_id` varchar(32) NOT NULL, `bank_name` varchar(64) NOT NULL, `blob_data` blob NOT NULL, PRIMARY KEY (`primary_id`

Java 存储和读取 oracle CLOB 类型字段的实用方法

package oracle.clob; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.sql.Connection; import java.sql.DriverManager; import java

springmvc和servlet下的文件上传和下载(存文件目录和存数据库Blob两种方式)

项目中涉及了文件的上传和下载,以前在struts2下做过,今天又用springmvc做了一遍,发现springmvc封装的特别好,基本不用几行代码就完成了,下面把代码贴出来: FileUpAndDown.jsp <%@ page language="java" contentType="text/html; charset=UTF-8"%> <html> <head> <title>using commons Uplo

java se之File类

遍历某个目录路径下的所有文件并打印输出: package com.led.file; import java.io.File; public class File_List { public static void listFiles(File file){ if(file!=null){ if(file.isDirectory()){//是目录 File[] f=file.listFiles(); if(f!=null){ for(int i=0;i<f.length;i++){ listFi

springmvc和servlet在上传和下载文件(保持文件夹和存储数据库Blob两种方式)

参与该项目的文件上传和下载.一旦struts2下完成,今天springmvc再来一遍.发现springmvc特别好包,基本上不具备的几行代码即可完成,下面的代码贴: FileUpAndDown.jsp <%@ page language="java" contentType="text/html; charset=UTF-8"%> <html> <head> <title>using commons Upload to