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);
    }
    }

  • 相关阅读:
    bzoj4753: [Jsoi2016]最佳团体(分数规划+树形依赖背包)
    bzoj2956: 模积和(数论)
    51nod 1766 树上的最远点对(线段树)
    bzoj2621: [Usaco2012 Mar]Cows in a Skyscraper(状压DP)
    Codeforces Round #441 Div. 2题解
    bzoj4569: [Scoi2016]萌萌哒(ST表+并查集)
    iOS和Android后台机制对比
    UIApplicationDelegate 各方法回调时机
    iOS OC和JS的交互 javaScriptCore方法封装
    iOS应用的执行原理
  • 原文地址:https://www.cnblogs.com/demo-deng/p/8276946.html
Copyright © 2011-2022 走看看