Thursday, December 15, 2005
Java Code for Oracle CLOB
//this program is for writing to clob and reading from clob column.
//for running this program include ojdbc14.jar available in oracle site.
class TestOraCLobInsert {
public static void main (String args []) throws SQLException {
try {
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@server:1521:sid", "uid", "pwd");
PreparedStatement ps = conn.prepareStatement("INSERT INTO CLOBTABLE VALUES (?)");
oracle.sql.CLOB newClob = oracle.sql.CLOB.createTemporary(conn, false, oracle.sql.CLOB.DURATION_CALL);
newClob.putString(1,"This string, up to 4 gigabytes will be inserted into the CLOB");
ps.setClob(1, newClob);
int rowcnt = ps.executeUpdate();
System.out.println("Successful update of "+rowcnt+" row");
ps.close();
// for reading the data from the clob data type
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from t_temp");
while(rs.next()){
int i = rs.getInt(1);
oracle.sql.CLOB objClob = (oracle.sql.CLOB)rs.getClob(2);
InputStream objIS = objClob.getAsciiStream();
ByteArrayOutputStream objByteArrayOutputStream = new ByteArrayOutputStream(objIS.available());
byte b[] = new byte[1024];
int intReadBytes;
while((intReadBytes=objIS.read(b, 0,objIS.available() )) != -1) {
objByteArrayOutputStream.write(b, 0, intReadBytes);
}
System.out.println(objByteArrayOutputStream.toString());
}
conn.close();
}
catch (Exception e) {
System.out.println("Java Exception caught, error message="+e.getMessage());
}
}
}
//for running this program include ojdbc14.jar available in oracle site.
class TestOraCLobInsert {
public static void main (String args []) throws SQLException {
try {
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@server:1521:sid", "uid", "pwd");
PreparedStatement ps = conn.prepareStatement("INSERT INTO CLOBTABLE VALUES (?)");
oracle.sql.CLOB newClob = oracle.sql.CLOB.createTemporary(conn, false, oracle.sql.CLOB.DURATION_CALL);
newClob.putString(1,"This string, up to 4 gigabytes will be inserted into the CLOB");
ps.setClob(1, newClob);
int rowcnt = ps.executeUpdate();
System.out.println("Successful update of "+rowcnt+" row");
ps.close();
// for reading the data from the clob data type
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from t_temp");
while(rs.next()){
int i = rs.getInt(1);
oracle.sql.CLOB objClob = (oracle.sql.CLOB)rs.getClob(2);
InputStream objIS = objClob.getAsciiStream();
ByteArrayOutputStream objByteArrayOutputStream = new ByteArrayOutputStream(objIS.available());
byte b[] = new byte[1024];
int intReadBytes;
while((intReadBytes=objIS.read(b, 0,objIS.available() )) != -1) {
objByteArrayOutputStream.write(b, 0, intReadBytes);
}
System.out.println(objByteArrayOutputStream.toString());
}
conn.close();
}
catch (Exception e) {
System.out.println("Java Exception caught, error message="+e.getMessage());
}
}
}
Subscribe to Comments [Atom]