• 数据库读取和写入大文件



    针对大文本: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);
    }
    }

  • 相关阅读:
    记sql语句
    thinkPHP小记
    php操作目录和文件
    ubuntu搭配lamp
    phpStorm实用快捷键
    在wamp中www的文件夹(项目)打不开解决办法
    php正则表达式
    远程桌面,出现身份验证错误,要求的函数不正确,这可能是由于CredSSP加密Oracle修正
    discuz增加中文验证码
    react webpack 环境配置
  • 原文地址:https://www.cnblogs.com/demo-deng/p/8276946.html
Copyright © 2020-2023  润新知