20160408javaweb之JDBC 大二進位制和大檔案存取

破玉發表於2016-04-08

一、大文字存取:

我們有一本約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);
       }
 }
}

 

三、其中出現的問題見連結:

使勁點我啊

 

相關文章