加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

java操作BLOB读取

(2013-03-05 15:00:29)
标签:

java操作blob读取

java如何读取大对象数

oracle读取文本

java读取文本操作示例

分类: 数据库
package test;   
  
import java.io.BufferedInputStream;   
import java.io.FileInputStream;   
import java.io.PrintStream;   
import java.sql.Connection;   
import java.sql.DriverManager;   
import java.sql.ResultSet;   
import java.sql.SQLException;   
import java.sql.Statement;   
  
public class Test {   
    private Connection conn;   
  
      
    public Connection getConnection() {   
        try {   
            Class.forName("oracle.jdbc.driver.OracleDriver");   
            conn = DriverManager.getConnection(   
                    "jdbc:oracle:thin:@localhost:1521:lyx", "scott", "tiger");   
        } catch (ClassNotFoundException e) {   
            // TODO Auto-generated catch block   
            e.printStackTrace();   
        } catch (SQLException e) {   
            // TODO Auto-generated catch block   
            e.printStackTrace();   
         
        return conn;   
     
  
      
    public int insertImage(String path) throws Exception {   
        int i = 0;   
        Statement st = null;   
        ResultSet rs = null;   
        conn=this.getConnection();   
           
        conn.setAutoCommit(false);//设置数据库为不自动提交,必须的一步   
        st = conn.createStatement();   
        //先插入一个空对象,这里我调用了Empty_BLOB()函数   
        i = st.executeUpdate("insert into image (id,image) values (seq1.nextval,Empty_BLOB())");   
        //以行的方式锁定   
        rs = st.executeQuery("select image from image where id=(select max(id) from image) for update");   
        if (rs.next()) {   
            //得到流   
            oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(1);   
            //从得到的低级流构造一个高级流   
            PrintStream ps = new PrintStream(blob.getBinaryOutputStream());   
            BufferedInputStream bis = new BufferedInputStream(   
                    new FileInputStream(path));   
            byte[] buff = new byte[1024];   
            int n = 0;   
            //从输入到输出   
            while ((n = bis.read(buff)) != -1) {   
                ps.write(buff, 0, n);   
  
             
            //清空流的缓存   
            ps.flush();   
            //关闭流,注意一定要关   
            ps.close();   
            bis.close();   
         
        rs.close();   
        st.close();   
        conn.close();   
        return i;   
     
  
    public static void main(String[] args) throws Exception {   
        Test test=new Test();   
        test.insertImage("e:\\3.jpg");   
        System.out.println("OK");   
  
  
     
  
  

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有