当前位置:Gxlcms > 数据库问题 > jdbc—CLOB和BLOB

jdbc—CLOB和BLOB

时间:2021-07-01 10:21:17 帮助过:27人阅读

/** * 测试CLOB(文本大对象)的使用 * @author yangf * */ public class Demo09 { public static void main(String[] args) { Connection con = null; PreparedStatement ps1 = null; PreparedStatement ps2 = null; ResultSet rs = null; try { // 加载数据库驱动 Class.forName("com.mysql.jdbc.Driver"); // 获得connection对象 建立与数据库连接 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "123456"); ps1 = con.prepareStatement("insert into t_user (username,myInfo) values (?,?)"); ps1.setString(1, "yangf"); ps1.setClob(2, new FileReader("d:a.txt")); // 或者将程序中的字符串登录到DB中 ps1.setClob(2, new BufferedReader(new InputStreamReader(new ByteArrayInputStream("yangfyangf".getBytes())))); ps2 = con.prepareStatement("select * from t_user where id = ?"); ps2.setInt(1, 22009); rs = ps2.executeQuery(); while (rs.next()) { Clob c = rs.getClob("myInfo"); Reader r = c.getCharacterStream(); int len = 0; while (-1 != (len = r.read())) { System.out.print((char) r.read()); } } ps1.execute(); } catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps1 != null) { try { ps1.close(); } catch (SQLException e) { e.printStackTrace(); } } if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }

 

- BLOB(Binary Large Object)

  - 用于存储大量的二进制数据

  - 大字段有些特殊,不同数据库处理的方式不一样。大字段的操作常常是以流的方式来处理进行的。而非一般的字段,可以一次性读出数据。

- Mysql中相关类型

  - TINYBLOB最大长度为255(2^8 - 1)字节的BLOB列。

  - BLOB(M)最大长度为65535(2^16 - 1)字节的BLOB列。

  - MEDIUMBLOB最大长度为16777215(2^24 - 1)字节的BLOB列。

  - LONGBLOB最大长度为4294967295或4GB(2^32 - 1)字节的BLOB列。

例子:将大数据存储到DB的BLOB字段中,从DB中取出BLOB字段的内容。

  

package com.yf.jdbc.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 测试CLOB(文本大对象)的使用
 * @author yangf
 *
 */
public class Demo10 {
    
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps1 = null;
        PreparedStatement ps2 = null;
        ResultSet rs = null;
        try {
            // 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 获得connection对象 建立与数据库连接
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
            ps1 = con.prepareStatement("insert into t_user (username,headImg) values (?,?)");
            
            ps1.setString(1, "yangf123");
            ps1.setBlob(2, new FileInputStream("d:/head.jpg"));
            ps1.execute();
            
            // 取出数据库的BLOB信息
            ps2 =con.prepareStatement("select * from t_user where id = ?");
            ps2.setInt(1, 22010);
            rs = ps2.executeQuery();
            while (rs.next()) {
                Blob blob = rs.getBlob("headImg");
                InputStream is = blob.getBinaryStream();
                int len = 0;
                FileOutputStream os = new FileOutputStream("d:/yangf.jpg");
                while ((len = is.read()) != -1) {
                    os.write(len);
                }
            }
            
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps1 != null) {
                try {
                    ps1.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

jdbc—CLOB和BLOB

标签:cte   ring   ade   nbsp   str   exce   fileinput   []   mysq   

人气教程排行