9 大文本对象插入数据库(CLOB)

import java.io.*;
import java.sql.*;
/*
 * CLOB(Character Large Object)
 * CLOB文本大对象操作
 *   用于存储大量的文本数据
 *      大字段有些特殊,不同数据库处理方式不一样,
 *      大字段的操做一般是以流的方式来处理,而非一般的字段,一次即可读取数据
 *  
 *  TINYTEXT,TINYBLOB
 *      一个BLOB或TEXT列,最大长度为255(2^8-1)个字符。
 *  BLOB,TEXT
 *      一个BLOB或TEXT列,最大长度为65535(2^16-1)个字符。
 *  MEDIUMBLOB,MEDIUMTEXT,
 *      一个BLOB或TEXT列,最大长度为16777215(2^24-1)个字符。
 *  LONGBLOB,LONGTEXT
 *      一个BLOB或TEXT列,最大长度为4294967295(2^32-1)个字符。
 */
public class TestCLOB {
    
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Reader r = null;
        
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/test","root","123456");
            
            
            ps = conn.prepareStatement(
                    "insert into user (name,myInfo) values (?,?)");
            ps.setString(1, "张三");
            
            //将文本中的内容直接输入到数据库中
            ps.setClob(2, new FileReader(new File("E:/jar/1.txt")));
            ps.executeUpdate();
            
            /*
             *  //将程序中的字符串输入到数据库的CLOB字段中
               //1:从内到外,首先,将"aaabbb"字符串转换为字节流,然后转为输入流,将输入流转为字符流,就可以读取
             * ps.setClob(2, new BufferedReader(new InputStreamReader(
                    new ByteArrayInputStream("aaabbb".getBytes()))));
             */
            
            //读取写入的内容
            ps = conn.prepareStatement("select * from user where name=?");
            ps.setObject(1, 2);
            
            rs = ps.executeQuery();
            
            while(rs.next()){
                Clob c = rs.getClob("myInfo");
                r = c.getCharacterStream();
                int temp = 0;
                while((temp = r.read()) != -1){
                    System.out.println((char)temp);
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(r != null){
                try {
                    r.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }   
    }
}


你可能感兴趣的:(9 大文本对象插入数据库(CLOB))