zoukankan      html  css  js  c++  java
  • jdbc 大数据存储 图片读取

    package com.itheima.clob.test;
    
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.Reader;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    import org.junit.Test;
    
    import com.itheima.utils.JdbcUtil;
    /**
     * 测试大文本数据读写
     drop database day16;
     create database day16;
     use day16;
     create table testclob(
         id int primary key,
         content longtext
     );
     * @author wangli
     *
     */
    public class ClobTest {
    
        @Test  //写入
        public void testClobAdd(){
            Connection con = null;
            PreparedStatement st =null;
            
            try {
                con = JdbcUtil.getConnection();
                String sql="insert into testclob values(?,?)";
                st = con.prepareStatement(sql);
                
                //赋值
                st.setInt(1, 2);
                //st.setString(2, "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");
                
                File file = new File("src/jdbccfg.properties");
                FileReader reader = new FileReader(file);//得到一个文件读取器
                st.setCharacterStream(2, reader, (int)file.length());//int jdk5,  long jdk1.6+
                                                            //MYsql支持的长度有限  ,改成int类型就可以了
                                                            //long类型,可以给oracle用
                
                st.executeUpdate();
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                JdbcUtil.release(null, st, con);
            }
        }
        
        
        @Test  //读取
        public void testClobReader(){
            Connection con = null;
            PreparedStatement st =null;
            ResultSet rs = null;
            try {
                con = JdbcUtil.getConnection();
                String sql="select * from textclob where id=?";
                st = con.prepareStatement(sql);
                
                //赋值
                st.setInt(1, 2);
                
                //3.执行查询
                rs = st.executeQuery();
                if(rs.next()){
                    //说明结果集中有记录
                    Reader reader = rs.getCharacterStream("content");
                    FileWriter fw = new FileWriter("d:/jdbc.properties");
                    int len=-1;
                    char [] buffer = new char[1024];
                    while((len=reader.read(buffer))!=-1){
                        fw.write(buffer, 0, len);
                    }
                    
                    //关闭流
                    fw.close();
                    reader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                JdbcUtil.release(null, st, con);
            }
        }
    }
  • 相关阅读:
    简单计算器--hdu1237(栈的运用)
    Bone Collector
    Red and Black---hdu1312(dfs)
    RTMP规范简单分析
    FFMPEG中最关键的结构体之间的关系
    面向对象与形而上学
    洛谷 P2913 [USACO08OCT]车轮旋转Wheel Rotation
    洛谷 P1889 士兵站队
    洛谷 P1885 Moo
    洛谷 P1683 入门
  • 原文地址:https://www.cnblogs.com/baijin05/p/5069301.html
Copyright © 2011-2022 走看看