Java存取文件 - Oracle

10 篇文章 0 订阅
2 篇文章 0 订阅

  CREATE TABLE "SCOTT"."TB_BLOB" 
   (	"ID" NUMBER, 
	"NAME" VARCHAR2(20 BYTE), 
	"SIZE" NUMBER, 
	"FILE" BLOB
   )


package com.tang;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import oracle.sql.BLOB;

public class TestOracle {
	
	static Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
			String user = "scott";
			String password = "123456";
			conn = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	//将文件从文件夹存储到Oracle
	public void testInsert(){
		Connection conn = TestOracle.getConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			conn.setAutoCommit(false);
			boolean defaultCommit = conn.getAutoCommit();
			st = conn.createStatement();
			File inputFile = new File("H:\\test1.txt");
			String fileName = inputFile.getName();
			long size = (long) inputFile.length();
			String sql = "insert into tb_blob(\"ID\",\"NAME\",\"SIZE\",\"FILE\") values" +
					"(1,'" + fileName + "',"+ size +",empty_blob())";
			st.executeUpdate(sql);
			sql = "select * from tb_blob  where id = 1 for update";
			rs = st.executeQuery(sql);
			if(rs.next()){
				BLOB blob = (BLOB)rs.getBlob("FILE");
				OutputStream outStream = blob.getBinaryOutputStream();
				InputStream fin = new FileInputStream(inputFile);
				byte[] b = new byte[blob.getBufferSize()];
				int len = 0;
				while((len = fin.read(b))!=-1){
					outStream.write(b,0,len);
				}
				fin.close();
				outStream.flush();
				conn.commit();
				conn.setAutoCommit(defaultCommit);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}  catch(FileNotFoundException e){
			e.printStackTrace();
		}  catch(IOException e){
			e.printStackTrace();
		}  finally{
			try {
				if(conn!=null){conn.close();} 
	 			if(st!=null){st.close();}
	 			if(rs!=null){rs.close();}
			}
 			catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	//将文件从Oracle读出到文件夹
	private void testQuery() {
		Connection conn = null;
		Statement st  = null;
		ResultSet rs = null;
		try{
			conn = this.getConnection();
			st = conn.createStatement();
			String sql = "select * from tb_blob where id = 1";
			rs = st.executeQuery(sql);
			if(rs.next()){
				Blob blob = rs.getBlob("FILE");
				InputStream reader = blob.getBinaryStream();
				OutputStream writer = new BufferedOutputStream(new FileOutputStream(new File("H:\\test1_fromDB.txt")));
				byte b[] = new byte[1024];
				for(int i = 0; (i = reader.read(b)) > 0 ;)
					writer.write(b, 0, i);
				writer.close();
				reader.close();
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}  catch(FileNotFoundException e){
			e.printStackTrace();
		}  catch(IOException e){
			e.printStackTrace();
		}  finally{
			try {
				if(conn!=null){conn.close();} 
	 			if(st!=null){st.close();}
	 			if(rs!=null){rs.close();}
			}
 			catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	 
	public static void main(String[] args) throws Exception {
		
		new TestOracle().testInsert();
		 
		new TestOracle().testQuery();
		System.out.println("Done!");
		

	}

	

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值