°´Ã¼Á÷·Äȸ¦ ÅëÇØ Á÷·ÄÈµÈ °´Ã¼¸¦ Oracle BLOB¿¡ ÀúÀåÇϱâ
ÀÚ¹ÙÀÇ °´Ã¼Á÷·Äȸ¦ ÀÌ¿ëÇϸé Á÷·ÄÈµÈ °´Ã¼¸¦ ÆÄÀÏ, µ¥ÀÌÅͺ£À̽º ¶Ç´Â ¿ø°ÝÀ¸·Î Àü¼Û ÇÏ´Â °ÍÀÌ °¡´É ÇÕ´Ï´Ù.
¹°·Ð °´Ã¼Á÷·ÄÈ°¡ µÇ±â À§Çؼ± Object´Â ÀÚ¹ÙÀÇ Seriablizable À̶ó´Â Interface¸¦ ±¸ÇöÇØ¾ß ÇÕ´Ï´Ù. ±×·¡¼ ÀÌ Object´Â Byte Stream ÇüÅ·ΠÀúÀå µÇ¾î ÆÄÀÏÀ̳ª DB¿¡ ÀúÀå µÈ ÈÄ ³ªÁß¿¡ LoadµÇ¾î¼ º¹¿øÀÌ °¡´É ÇÏ°Ô µÇ´Â °ÍÀÔ´Ï´Ù.
¾Æ·¡ÀÇ ¿¹Á¦´Â ObjectSerTest ¶ó´Â Ŭ·¡½º¸¦ Á÷·ÄÈ °¡´ÉÇÏ°Ô ¸¸µç ÈÄ ¿À¶óŬÀÇ BLOB¿¡ ÀÔ·Â ÇÑ ÈÄ Àо´Â ¿¹Á¦ ÀÔ´Ï´Ù.
=====================================================
SQL>conn scott/tiger
SQL> create sequence object_ser_seq increment by 1 start with 1;
ÁÖ¹®¹øÈ£°¡ »ý¼ºµÇ¾ú½À´Ï´Ù.
SQL> create table object_table (
2 no number,
3 obj_name varchar2(2000),
4 obj_value blob default empty_blob()
5 );
Å×À̺íÀÌ »ý¼ºµÇ¾ú½À´Ï´Ù.
=================================================
/*
* Created on 2005. 2. 11
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package jdbc;
import java.io.*;
import java.sql.*;
import oracle.sql.*;
class ObjectSerTest implements java.io.Serializable{
static final String driver_class = "oracle.jdbc.driver.OracleDriver";
static final String connectionURL = "jdbc:oracle:thin:@localhost:1521:wink";
static final String userID = "scott";
static final String userPassword = "tiger";
static final String getSequenceSQL = "SELECT object_ser_seq.nextval FROM dual";
static final String writeObjSQL = "BEGIN " +
" INSERT INTO object_table(no, obj_name, obj_value) " +
" VALUES (?, ?, empty_blob()) " +
" RETURN obj_value INTO ?; " +
"END;";
static final String readObjSQL = "SELECT obj_value FROM object_table WHERE no = ?";
/*
** +--------------------------------------------------+
** | METHOD: writeObj |
** +--------------------------------------------------+
*/
public static long writeObj(Connection conn, Object obj) throws Exception {
long id = getNextSeqVal(conn);
String className = obj.getClass().getName();
CallableStatement stmt = conn.prepareCall(writeObjSQL);
stmt.setLong(1, id);
stmt.setString(2, className);
stmt.registerOutParameter(3, java.sql.Types.BLOB);
stmt.executeUpdate();
BLOB blob = (BLOB) stmt.getBlob(3);
OutputStream os = blob.getBinaryOutputStream();
ObjectOutputStream oop = new ObjectOutputStream(os);
oop.writeObject(obj);
oop.flush();
oop.close();
os.close();
stmt.close();
System.out.println("Done serializing: " + className);
return id;
} // END: writeObj
/*
** +--------------------------------------------------+
** | METHOD: readObj |
** +--------------------------------------------------+
*/
public static Object readObj(Connection conn, long id) throws Exception {
PreparedStatement stmt = conn.prepareStatement(readObjSQL);
stmt.setLong(1, id);
ResultSet rs = stmt.executeQuery();
rs.next();
InputStream is = rs.getBlob(1).getBinaryStream();
ObjectInputStream oip = new ObjectInputStream(is);
Object obj = oip.readObject();
String className = obj.getClass().getName();
oip.close();
is.close();
stmt.close();
System.out.println("Done de-serializing: " + className);
return obj;
} // END: readObj
/*
** +--------------------------------------------------+
** | METHOD: getNextSeqVal |
** +--------------------------------------------------+
*/
private static long getNextSeqVal (Connection conn) throws SQLException {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(getSequenceSQL);
rs.next();
long id = rs.getLong(1);
rs.close();
stmt.close();
return id;
} // END: getNextSeqVal
/*
** +--------------------------------------------------+
** | METHOD: main |
** +--------------------------------------------------+
*/
public static void main (String args[]) throws SQLException {
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
int insertResults;
int deleteResults;
try {
System.out.print("\n");
System.out.print("Loading JDBC Driver -> " + driver_class + "\n");
Class.forName (driver_class).newInstance();
/*
** CONNECT TO THE DATABASE
*/
System.out.print("Connecting to -> " + connectionURL + "\n");
conn = DriverManager.getConnection(connectionURL, userID, userPassword);
System.out.print("Connected as -> " + userID + "\n\n");
/*
** TURN OFF AutoCommit
*/
conn.setAutoCommit(false);
ObjectSerTest obj = new ObjectSerTest();
long no = writeObj(conn, obj);
conn.commit();
System.out.print("Serialized OBJECT_ID => " + no + "\n\n");
System.out.print("OBJECT VALUES => " + readObj(conn, no) + "\n\n");
conn.close();
} // TRY:
catch (Exception e) {
e.printStackTrace();
}
finally {
if (conn != null) {
try {
System.out.print("Closing down all connections...\n\n");
conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
} // FINALLY:
} // METHOD: main
} // CLASS: ObjectSerTest
[°á°ú]
Loading JDBC Driver -> oracle.jdbc.driver.OracleDriver
Connecting to -> jdbc:oracle:thin:@localhost:1521:wink
Connected as -> scott
Done serializing: jdbc.ObjectSerTest
Serialized OBJECT_ID => 2
Done de-serializing: jdbc.ObjectSerTest
OBJECT VALUES => jdbc.ObjectSerTest@601bb1
Closing down all connections...