package cn.itcast.demo;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.junit.Test;

import cn.itcast.utils.JdbcUtils;

public class Demo1 {

    /**
     
     create database day15;
     use day15;
     create table testclob
     (
         id int primary key auto_increment,
         resume text
     );
      
     */
    
    //向Mysql数据库 中存储大文本数据

    @Test
    public void add() {
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        
        try{
            conn = JdbcUtils.getConnection();
            String sql = "insert into testclob(resume) values(?)";
            st = conn.prepareStatement(sql);
            
            //Reader reader = new InputStreamReader(Demo1.class.getClassLoader().getResourceAsStream("1.txt"));
            String path  = Demo1.class.getClassLoader().getResource("1.txt").getPath();
            File file = new File(path);
            st.setCharacterStream(1, new FileReader(file), (int) file.length());
            int num = st.executeUpdate();
            if(num>0){
                System.out.println("插入成功!!");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            JdbcUtils.release(conn, st, rs);
        }
    }
    
    @Test
    public void read(){
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        
        try{
            conn = JdbcUtils.getConnection();
            String sql = "select resume from testclob where id=1";
            st = conn.prepareStatement(sql);
            rs = st.executeQuery();
            if(rs.next()){
                System.out.println("haha");
                Reader reader = rs.getCharacterStream("resume");
                char buffer[] = new char[1024];
                int len = 0;
                FileWriter out = new FileWriter("c:\\1.txt");
                while((len=reader.read(buffer))>0){
                    out.write(buffer, 0, len);
                }
                out.close();
                reader.close();
            }
            
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            JdbcUtils.release(conn, st, rs);
        }
    }
    
    
}