自己改過的BLOB檔案的插入和讀取JAVA代碼

來源:互聯網
上載者:User

找了不少 BLOB的資料 不過沒有幾個可以調試成功 把 自己改他們的代碼發出來 大家可看看

寫入ORACLE資料庫

package com;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.sql.BLOB;

public class Jdbctest {
 
 public static void main(String[] args) throws SQLException, IOException {
  dbBean db = new dbBean();//自己的串連類
  Connection con = db.getConnect();
  con.setAutoCommit(false);
  PreparedStatement preStmt = con.prepareStatement("insert into testblob (id,blobs) values (?,?)");
  preStmt.setInt(1, 1);
  preStmt.setBlob(2, BLOB.empty_lob());
  preStmt.executeUpdate();
  preStmt.close();
  preStmt = con.prepareStatement("select blobs from testblob where id=? for update");
  preStmt.setInt(1, 1);
  ResultSet rest = preStmt.executeQuery();
  rest.next();
  BLOB blobb = (BLOB) rest.getBlob(1);
  FileInputStream instream = new FileInputStream("E:/test.txt");
  OutputStream outstream = blobb.getBinaryOutputStream();
  byte[] buf = new byte[1024000];
  int len;
  while ((len = instream.read(buf)) > 0) {
   outstream.write(buf, 0, len);
  }
  instream.close();
  outstream.close();
  preStmt = con.prepareStatement("update testblob set blobs=? where id=?");
  preStmt.setBlob(1, blobb);
  preStmt.setInt(2, 1);
  preStmt.executeUpdate();
  preStmt.close();
  con.commit();
  con.close();

 }

}

 

讀出來 寫入檔案

package com;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.sql.BLOB;

public class readfile {

 
 public static void main(String[] args) throws SQLException, IOException
 {
  File out=new File("E:/outtest.txt");
  java.io.FileOutputStream fout=new FileOutputStream(out);
  byte b[]=null;
  
  dbBean db = new dbBean();
  Connection con = db.getConnect();
  con.setAutoCommit(false);
  PreparedStatement preStmt = con.prepareStatement("select blobs from testblob where id=? for update");
  preStmt.setInt(1, 1);
  ResultSet rest = preStmt.executeQuery();
   if (rest.next())
         {
         oracle.sql.BLOB blob2=(BLOB)rest.getBlob(1);
         System.out.println("blob2 length:"+blob2.length());
         b=blob2.getBytes(1,(int)blob2.length());
         }
   fout.write(b,0,b.length);
  

 }

}

在ORACLE9。0。2調試通過 

很苦悶的事情 昨天 搞了一天 絲毫沒有什麼結果  今天上午老到公司 看了看 書  居然搞定之 比較HAPPY 特地發出來大家欣賞一下

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.