说明1:首先所有的文件都是以二进制存储
2:二进制文件有.doc .xls .jpg
文本文件有 .txt .html .xml
blob存储字节,clob存储字符
数据表的创建:
create table CDL_TEST(
SID varchar(20) not null,
IMG BLOB,
DOC CLOB,
DATA NUMBER
);
图片的存取:
、、、java
package store.img.to.oracle;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StoreImage {
private static String user ="System";
private static String password ="Admin";
private static String driver ="oracle.jdbc.driver.OracleDriver";
private static String url ="jdbc:oracle:thin:@localhost:1521:ORCL";
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConnetion() {
Connection conn=null;
try {
conn =DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(conn.hashCode());
return conn;
}
// public void insertBlob(String sql1) {
// Connection conn1 = getConnetion();
// ResultSet rs =null;
// try {
// conn1.setAutoCommit(false);
// Blob blob =null; //插入空的Blob
// PreparedStatement pstmt = conn1.prepareStatement("insert into cdl_test(sid,img) values (?,empty_blob())");
// pstmt.setString(1, "100");
// pstmt.executeUpdate();
// pstmt.close();
//
// rs=conn1.createStatement().executeQuery(sql1);
// while(rs.next())
// {
// System.out.println("rs length is: ");
// BLOB b =(BLOB) rs.getBlob("img");
// System.out.println("bloblegth is "+b.getLength());
// BufferedInputStream bi = new BufferedInputStream(new FileInputStream("file/img.jpg"));
// BufferedOutputStream out = new BufferedOutputStream(b.getBinaryOutputStream());
// int c ;
// while((c=bi.read())!=-1)
// {
// out.write(c);
// }
// }
// conn1.commit();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }finally {
// //流的关闭
// }
//
// }
/**
* 此方法有一个问题,那就是jdbc的所用的jar包要要求,jar包版本不能太高,最好和oracle中的数据库版本一致。,oracle是11的话,用11的jdbcjar包
*/
public void insertBlob() {
Connection conn1 = getConnetion();
PreparedStatement pstmt=null ;
try {
pstmt=conn1.prepareStatement("insert into cdl_test(sid,img) values ('1',?)");
File file = new File("file/img.jpg");
FileInputStream bi = new FileInputStream(file);
pstmt.setBinaryStream(1, bi, file.length());
pstmt.executeUpdate();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭资源
}
}
//读取图片
public void ReadBlob(String sql2) {
Connection conn = getConnetion();
FileOutputStream fos=null;
InputStream is=null;
ResultSet rs =null;
try {
System.out.println("sql2 is "+sql2);
System.out.println("stmt is "+conn);
rs= conn.createStatement().executeQuery(sql2);
while(rs.next())
{
System.out.println("rs length is ");
Blob b = (Blob)rs.getBlob("img");
File f = new File("D:/img.jpg");
fos = new FileOutputStream(f);
is = b.getBinaryStream();//读出数据后转为二进制流
byte[] date = new byte[1024];
while(is.read(date)!=-1)
{
fos.write(date);
}
}
conn.commit();
fos.close();
} catch (Exception e) {
// TODO: handle exception
}finally {
//流的关闭
}
}
public static void main(String[] args) {
String sql1 = "select * from cdl_test for update";
String sql2 ="select * from cdl_test";
StoreImage storeImage = new StoreImage();
storeImage.insertBlob();
storeImage.ReadBlob(sql2);
}
}
文本的存取:
package store.img.to.oracle;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.sql.CLOB;
public class StoreClob {
private static String user ="System";
private static String password ="Admin";
private static String driver ="oracle.jdbc.driver.OracleDriver";
private static String url ="jdbc:oracle:thin:@localhost:1521:ORCL";
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConnetion() {
Connection conn=null;
try {
conn =DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(conn.hashCode());
return conn;
}
//clob字段中添加数据
public void insertClob() {
Connection conn = getConnetion();
PreparedStatement pstmt=null ;
try {
pstmt=conn.prepareStatement("insert into cdl_test(sid,DOC) values ('2',?)");
String text = new String("我是要被存储在oracle的数据");
//将字符串转为字符流
Reader clobReader = new StringReader(text);
pstmt.setCharacterStream(1, clobReader,text.length());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭资源
}
}
//读取clob中的字段
public void readClob() {
Connection conn = getConnetion();
Statement pstmt=null ;
ResultSet rs=null;
Reader reader=null;
String readStr=null;
try {
pstmt=conn.createStatement();
rs = pstmt.executeQuery("select doc from cdl_test where sid='2' ");
while(rs.next())
{
//这里为什么要使用CLOB呢,CLOB才有length方法。Clob没有length方法
CLOB clob = (CLOB) rs.getClob("doc");
reader = clob.getCharacterStream();
char charbytes[] = new char[(int) clob.length()];
reader.read(charbytes);
readStr = new String(charbytes);
}
File file = new File("D:/text.txt");
if(file.exists())
{
file.delete();
}
file.createNewFile();
PrintWriter pw = new PrintWriter(new FileOutputStream(file));
pw.write(readStr);
pw.flush();
pw.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭资源
}
}
public static void main(String[] args) {
StoreClob clob = new StoreClob();
clob.insertClob();
clob.readClob();
}
}