Java存取文件 - Oracle
2015-10-17 16:25
295 查看
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!");
}
}
( "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!");
}
}
相关文章推荐
- Centos6.5上安装oracle11gR2 完整版
- oracle 11g 64w 用32位的pl/sql
- java操作oracle数据库的时间类型
- oracle 使用 date timestamp 而不使用varchar2()的好处
- Oracle 11g netca,dbca乱码之解决
- C#连接oracle的方式以及问题解决.
- MySQL、Oracle、SQL Server查询前n条记录
- Maven手动安装jar包-Oracle Jdbc
- Linux图形界面打开Oracle Net Manager
- [ArcGIS所需的补丁]ArcGIS 10.2.2 for Desktop联系Oracle(2014年10上个月发布)数据库崩溃
- Oracle --------时间转换函数(to_date)
- oracle启动脚本
- Oracle 管理中需要用到的命令
- Oracle SQL 调优健康检查脚本
- Windows server2008 搭建ASP接口访问连接oracle数据库全过程记录--备用
- Oracle SQL 调优之 sqlhc
- oracle 常用系统表
- Oracle数据库中的数据库名、全局数据库名、实例名、ORACLE_SID
- Oracle自定义函数fn_split,字符串中的#号去掉,instr和substr
- SQLPLUS登录oracle方式