자바 조작 Oracle 데이터베이스 의 Clob, Blob 필드
1:
2: .doc .xls .jpg
.txt .html .xml
oracle
create table CDL_TEST
(
SID VARCHAR2(20) not null,
IMG BLOB, //
DOC CLOB, //
DATA NUMBER
)
--
alter table CDL_TEST
add constraint CDL_SID primary key (SID)
:
package DataBaseUtil;
import java.sql.*;
import java.util.Scanner;
import java.io.*;
import oracle.sql.BLOB;
class InitDB{
private static Connection con=null;
private static Statement stmt=null;
private static ResultSet rs=null;
InitDB(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:ORCL"; //ORCL sid
String user="cdl";
String password="1";
con= (Connection) DriverManager.getConnection(url,user,password);
InitDB.setCon(con);
}catch(Exception e){
e.printStackTrace();
}
}
public void closCon(){
try{
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void stmt(){
try{
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void rs(){
try{
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static Connection getCon() {
return con;
}
public static void setCon(Connection con) {
InitDB.con = con;
}
public static ResultSet getRs() {
return rs;
}
public static void setRs(ResultSet rs) {
InitDB.rs = rs;
}
public static Statement getStmt() {
return stmt;
}
public static void setStmt(Statement stmt) {
InitDB.stmt = stmt;
}
}
/*
* Blob :
*/
class InsertBlobData{
private ResultSet rs=null;
private InitDB idb=null;
InsertBlobData(){
idb=new InitDB();
}
public void insertBlob(String sql1) throws SQLException{
Connection con=idb.getCon();
try{
con.setAutoCommit(false);//
BLOB blob = null; // Blob
PreparedStatement pstmt = con.prepareStatement("insert into cdl_test(sid,img) values(?,empty_blob())");
pstmt.setString(1,"100");
pstmt.executeUpdate();
pstmt.close();
rs=con.createStatement().executeQuery(sql1);
while(rs.next()){
System.out.println("rs length is:");
oracle.sql.BLOB b=(oracle.sql.BLOB )rs.getBlob("img");
System.out.println("cloblength is:"+b.getLength());
File f=new File("d:\\img\\1.jpg");
System.out.println("file path is:"+f.getAbsolutePath());
BufferedInputStream in=new BufferedInputStream(new FileInputStream(f));
BufferedOutputStream out=new BufferedOutputStream(b.getBinaryOutputStream());
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
con.commit();
}catch(Exception e){
con.rollback();//
e.printStackTrace();
}
}
}
/*
* :1.txt
*/
class InsertClobData{
private ResultSet rs=null;
private InitDB idb=null;
InsertClobData(){
idb=new InitDB();
}
public void insertClob(String sql1) throws SQLException{
Connection con=idb.getCon();
try{
con.setAutoCommit(false);//
BLOB blob = null; // Clob
PreparedStatement pstmt = con.prepareStatement("insert into cdl_test(sid,doc) values(?,empty_clob())");
pstmt.setString(1,"101");
pstmt.executeUpdate();
pstmt.close();
rs=con.createStatement().executeQuery(sql1);
while(rs.next()){
System.out.println("sdfasdfas");
oracle.sql.CLOB cb=(oracle.sql.CLOB)rs.getClob("doc");
File f=new File("d:\\doc\\1.txt");
System.out.println("file path is:"+f.getAbsolutePath());
BufferedWriter out = new BufferedWriter(cb.getCharacterOutputStream());
BufferedReader in = new BufferedReader(new FileReader(f));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
con.commit();
}catch(Exception e){
con.rollback();//
e.printStackTrace();
}
}
}
/*
*
*/
class ReadBlobData{
private ResultSet rs=null;
private InitDB idb=null;
ReadBlobData(){
idb=new InitDB();
}
public void getBlob(String sql2) throws SQLException{
Connection con=idb.getCon();
con.setAutoCommit(false);
try{
System.out.println("sq2 is:"+sql2);
System.out.println("stmt is:"+con);
rs=con.createStatement().executeQuery(sql2);
while(rs.next()){
System.out.println("rs length is:");
Blob b=(Blob)rs.getBlob("img");
File f=new File("D:\\saveimg\\1.jpg");
FileOutputStream fos=new FileOutputStream(f);
InputStream is=b.getBinaryStream();//
byte[] data=new byte[1024];
while(is.read(data)!=-1){
fos.write(data);
}
fos.close();
is.close();
}
con.commit();//
}catch(Exception e){
e.printStackTrace();
}finally{
//rs.close();
}
}
}
/*
*
*/
class ReadClobData{
private ResultSet rs=null;
private InitDB idb=null;
ReadClobData(){
idb=new InitDB();
}
public void getClob(String sql2) throws SQLException{
Connection con=idb.getCon();
try{
con.setAutoCommit(false);//
System.out.println("sq2 is:"+sql2);
rs=con.createStatement().executeQuery(sql2);
while(rs.next()){
oracle.sql.CLOB clob=(oracle.sql.CLOB)rs.getClob("doc");
File f=new File("d:\\savedoc\\1.txt");
BufferedReader in = new BufferedReader(clob.getCharacterStream());
//setCharacterStream() , CLOB ,
BufferedWriter out = new BufferedWriter(new FileWriter(f));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
out.close();
in.close();
}
con.commit();//
rs.close();
}catch(Exception e){
e.printStackTrace();
con.rollback();
}
}
}
public class TestBlob {
public static void main(String []args){
String sql1="select * from cdl_test for update";//
String sql2="select * from cdl_test";
System.out.println("\t\t\t :");
System.out.println("1: ");
System.out.println("2: ");
System.out.println("3: ");
System.out.println("4: ");
System.out.println("5: ");
System.out.println(" :")
while(true){
try{
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
System.out.println("sss:"+i);
switch(i){
case 1:
InsertBlobData isd=new InsertBlobData();
isd.insertBlob(sql1); break;
case 2:
InsertClobData icd=new InsertClobData();
icd.insertClob(sql1); break;
case 3:
ReadBlobData rb=new ReadBlobData();
rb.getBlob(sql2); break;
case 4:
ReadClobData rc=new ReadClobData();
rc.getClob(sql2); break;
case 5:
System.exit(0);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.