20160408javaweb之JDBC 大二进制和大文件存取

一、大文本存取:

我们有一本约10M的小说,现存入数据库:

代码如下:以junit测试的方式给出

 

package com.dzq.lob;

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

import org.junit.Test;

import com.dzq.util.JDBCUtils;

public class TextDemo1 {
@Test
public void addText(){
   Connection conn=null;
   PreparedStatement ps=null;
   ResultSet rs=null;
   String sql="insert into textdemo values (null,?,?)";
   try{
       conn=JDBCUtils.getConn();
       ps=conn.prepareStatement(sql);
       ps.setString(1, "钢铁是怎样炼成的.txt");
       File file=new File("1.txt");
       ps.setCharacterStream(2, new FileReader(file),(int)file.length());
       ps.executeUpdate();
   }catch(Exception e){
       e.printStackTrace();
   }finally{
       JDBCUtils.close(rs, ps, conn);
   }
}

@Test
public void findText(){
     
       Connection conn=null;
       PreparedStatement ps=null;
       ResultSet rs=null;
       String sql="select * from textdemo where id=?";
       try{
           conn=JDBCUtils.getConn();
           ps=conn.prepareStatement(sql);
           ps.setInt(1, 5);
           rs=ps.executeQuery();
           while (rs.next()) {
            String filename=rs.getString("name");
            Reader reader=rs.getCharacterStream("content");
            Writer writer=new FileWriter(filename);
            
            char []cs=new char[1024];
            int i=0;
            while ((i=reader.read(cs))!=-1) {
                writer.write(cs,0,i);
            }
            reader.close();
            writer.close();
        }
       }catch(Exception e){
           e.printStackTrace();
       }finally{
           JDBCUtils.close(rs, ps, conn);
       }
}
}

 

 

 

二、大二进制存取:

我们有一个mp3文件,现存入数据库

代码如下:

 

package com.dzq.lob;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.junit.Test;

import com.dzq.util.JDBCUtils;

public class BlobDemo1 {
 @Test
 public void addBlob(){
     Connection conn=null;
       PreparedStatement ps=null;
       ResultSet rs=null;
       String sql="insert into blobdemo values (null,?,?)";
       try{
           conn=JDBCUtils.getConn();
           ps=conn.prepareStatement(sql);
           ps.setString(1, "洛天依.mp3");
           File file=new File("1.mp3");
           ps.setBinaryStream(2, new FileInputStream(file),(int)file.length());
           ps.executeUpdate();
       }catch(Exception e){
           e.printStackTrace();
       }finally{
           JDBCUtils.close(rs, ps, conn);
       }
     
 }
 @Test
 public void findBlob(){
     Connection conn=null;
       PreparedStatement ps=null;
       ResultSet rs=null;
       String sql="select * from blobdemo";
       try{
           conn=JDBCUtils.getConn();
           ps=conn.prepareStatement(sql);
           rs=ps.executeQuery();
           while (rs.next()) {
            String filename=rs.getString("name");
            InputStream in=rs.getBinaryStream("content");
            OutputStream out=new FileOutputStream(filename);
            
            byte []bs=new byte[1024];
            int i=0;
            while ((i=in.read(bs))!=-1) {
                out.write(bs,0,i);
            }
            in.close();
            out.close();
        }
       }catch(Exception e){
           e.printStackTrace();
       }finally{
           JDBCUtils.close(rs, ps, conn);
       }
 }
}

 

三、其中出现的问题见链接:

使劲点我啊

 

posted @ 2016-04-08 18:26 破玉 阅读(...) 评论(...) 编辑 收藏