zoukankan      html  css  js  c++  java
  • 数据库读取和写入大文件


    针对大文本:Clob:字符大对象:Character large object
    // file.txt的文件保存到数据库中
    @Test
    public void test(){
    Connction conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try{
    conn = JdbcUtil.getConnection();
    stmt = conn.prepareStatement("insert into count(id,content) values (?,?)");
    stmt.setInt(1,1);
    // 使用字符流的形式存储,提高效率
    File file = new File("c:/file.txt");
    Reader reader = new FileReader(file);
    stmt.setCharacterStream(2,reader, (int)file.length());
    stmt.executeUpdate();
    }catch(Exception e){
    throw new RuntimeException(e);
    }finally{

    JdbcUtil.release(rs, stmt, conn);
    }
    }
    // 将id=1的记录content的内容写到磁盘上
    @Test
    public void test(){
    Connction conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try{
    conn = JdbcUtil.getConnection();
    stmt = conn.prepareStatement("select * from count where id=1");
    // 查询结果集
    rs = stmt.executeQuery();
    if(rs.next()){
    // 得到结果字符流
    Reader reader = rs.getCharacterStream("content");
    // 得到一只笔,写入到文件中
    Writer writer = new Writer("c:/deng.txt");
    // 开始写入
    char[] buf = new char[1024];
    int len = -1;
    while((len=reader.read(buf))!=-1){
    writer.write(buf,0,len);
    }
    writer.close();
    reader.close();
    }
    }catch(Exception e){
    throw new RuntimeException(e);
    }finally{

    JdbcUtil.release(rs, stmt, conn);
    }
    }

    // 大二进制数据:语音、视频、图片、压缩包.Blob:二进制大对象 Binary Large Object
    // 将1.jpg保存到数据库中
    @Test
    public void test(){
    Connction conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try{
    conn = JdbcUtil.getConnection();
    stmt = conn.prepareStatement("insert into t2(id, content) values (?,?)");
    stmt.setInt(1,1);
    // 使用字节流传输数据,提高工作效率
    InputStream in = new FileInputStream("c:/1.jpg");
    stmt.setBinaryStream(2,in, in.available());
    stmt.executeUpdate();

    }catch(Exception e){
    throw new RuntimeException(e);
    }finally{

    JdbcUtil.release(rs, stmt, conn);
    }
    }
    // 把content的内容写到磁盘上

    @Test
    public void test(){
    Connction conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try{
    conn = JdbcUtil.getConnection();
    stmt = conn.prepareStatement("select * from count where id=1");
    // 查询结果集
    rs = stmt.executeQuery();
    if(rs.next()){
    // 得到结果输入字节流
    InputStream in = rs.getBinaryStream("content");
    // 字节输出流,将内容输出到对应的文件
    OutputStream out = new FileOutputStream("c:/deng.txt");
    // 开始写入
    byte[] buf = new byte[1024];
    int len = -1;
    while((len=in.read(buf))!=-1){
    out.write(buf,0,len);
    }
    out.close();
    in.close();
    }
    }catch(Exception e){
    throw new RuntimeException(e);
    }finally{

    JdbcUtil.release(rs, stmt, conn);
    }
    }

  • 相关阅读:
    day38 04-Spring加载配置文件
    day38 03-Spring的IOC和DI的区别
    day38 02-Spring快速入门
    关于mysql-connector-net在C#中的用法
    SQL的四种连接用法整理
    SQL的四种连接用法整理
    SQL的四种连接用法整理
    45道CSS基础面试题
    45道CSS基础面试题
    45道CSS基础面试题
  • 原文地址:https://www.cnblogs.com/demo-deng/p/8276946.html
Copyright © 2011-2022 走看看