欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java序列化对象存储ORACLE数据库

程序员文章站 2022-06-02 13:02:51
...

****package com.csii.ibs.oracle;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.sql.BLOB;
/**
* @Title: BlobTestCase.java
* @Package com.csii.ibs.oracle
* @Description: Blob插入数据库测试
* @author lr
* @date 2017年9月20日 下午3:32:23
* @modify
* @version V1.0
*/
public class BlobTestCase {
private static String url,name,password;
private static Connection con = null;
private static PreparedStatement pre = null;
private static ResultSet result = null;
private static ClassLoader loader;
private static BlobTestCase obj;
public BlobTestCase() throws ClassNotFoundException, SQLException {
Class.forName(“oracle.jdbc.OracleDriver”);
this.ObtainConnect();
}
static {
url = “jdbc:oracle:thin:@localhost:1521:ebankdb”;
password = “pibs”;
name = “pibs”;
if(null==obj)
try {
obj = new BlobTestCase();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
loader = BlobTestCase.class.getClassLoader();
}
public void ObtainConnect() {
if(null==con)
try {
con = DriverManager.getConnection(url, name, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings(“unused”)
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IOException {
Object o = obj;
String className = “com.csii.ibs.oracle.Car”;
//插入
// String insertsql = “insert into pibs.testblob (NAME,CONTENT,ID) values (?,empty_blob(),null)”;
// obj.insertBlob(obj.loadingClass(className), insertsql);
//查询
String readsql = “select CONTENT from pibs.testblob where NAME=?”;
String blobname = “CONTENT”;
obj.ReadBlob(readsql,blobname);
}
//插入Blob
public void insertBlob(Object clsobj,String sql) throws InstantiationException, IllegalAccessException, IOException {
try {
ByteArrayOutputStream bao = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bao);
oos.writeObject(clsobj);
pre = con.prepareStatement(sql);
pre.setString(1, “aa”);
pre.executeUpdate();
String qry = “select CONTENT from pibs.testblob where name=? for update”;
pre = con.prepareStatement(qry);
pre.setString(1, “aa”);
result = pre.executeQuery();
if(result.next()) {
BLOB blob = (BLOB) result.getBlob(“CONTENT”);
OutputStream os = blob.getBinaryOutputStream();
try {
os.write(bao.toByteArray(),0,bao.toByteArray().length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
os.flush();
os.close();
}
oos.close();
bao.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
close();
}
}
@SuppressWarnings(“rawtypes”)
//动态写入指定类数据
public Object loadingClass(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
Class cls = loader.loadClass(“com.csii.ibs.oracle.Car”);
Object obj = cls.newInstance();
Method method;
method = cls.getMethod(“setName”, String.class);
method.invoke(obj, “ppp”);
return obj;
}
//动态读取指定类数据
public void writeClass(Object obj) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
Method method;
method = obj.getClass().getMethod(“getName”);
String test = (String) method.invoke(obj, null);
System.out.println(test);
}
//更新Blob

//查询Blob
 public void ReadBlob(String sql,String blobname) {
     try {
        pre = con.prepareStatement(sql);
        pre.setString(1, "aa");
        result = pre.executeQuery();
        if(result.next()) {
            BLOB blob = (BLOB) result.getBlob(blobname);
            InputStream is = blob.getBinaryStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            byte[] buf = new byte[blob.getBufferSize()];
            try {
                while(-1!=(bis.read(buf, 0, buf.length)));
                ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buf, 0, buf.length));
                Object ojj = ois.readObject();
                try {
                    this.writeClass(ojj);
                } catch (InstantiationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } catch (SQLException e) {
        close();
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }

//关闭连接
 public void close() {
        try {
            result.close();
            pre.close();
            con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 }

}**