Hi,
Access string type of data from oracle is easy, but access blob type is not. here is one example, requires: JDK1.4, classes111.zip from oracle 8.1.5 +,
import java.sql.;
import java.io.;
import java.util.;
import oracle.jdbc.driver.;
//needed for new CLOB and BLOB classes
import oracle.sql.*;
public class JBlob {
public static void main(String args[]) throws Exception {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn=DriverManager.getConnection("jdbcracle:thin123.123.123.123:1521rcl", "dbusername", "dbpassword");
conn.setAutoCommit (false);
Statement stmt = conn.createStatement();
try {
stmt.execute ("drop table SessionTable");
System.out.println("Table droped ...");
}
catch (Exception e) {
System.out.println("Table does not exist");
}
System.out.println("creating table");
// Create the table
stmt.execute ("create table SessionTable (mysession varchar2(200), myobject BLOB)");
System.out.println("Table created ...");
stmt.execute("insert into SessionTable values('session1', empty_blob())");
stmt.execute("commit");
System.out.println("inserted");
String cmd = "select mysession, myobject from SessionTable for update";
oracle.jdbc.driver.OracleResultSet rset = ( oracle.jdbc.driver.OracleResultSet)stmt.executeQuery(cmd);
System.out.println("reading...");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject("jack su yu testing string");
oout.writeInt(134);
byte buf[] = bout.toByteArray();
while (rset.next()) {
BLOB blob = ((OracleResultSet)rset).getBLOB(2);
OutputStream out = blob.getBinaryOutputStream();
int chunk = blob.getChunkSize();
int pos = 0;
while (pos < buf.length) {
out.write(buf, pos, chunk);
pos += chunk;
}
out.close();
System.out.println("writen to blob");
InputStream in = blob.getBinaryStream();
bout = new ByteArrayOutputStream();
buf = new byte[chunk];
int l;
while ((l = in.read(buf)) != -1) {
bout.write(buf, 0, l);
}
buf = bout.toByteArray();
in.close();
ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(buf));
System.out.println(oin.readObject());
System.out.println(oin.readInt());
}
stmt.close();
}
}
No one has replied yet! Why not be the first?
Sign in or Join us (it's free).