import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.CallableStatement;
import oracle.sql.CLOB;
import java.io.Writer;
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
public class TestClob {
static String id = null;
static String fileName = null;
static String streamingFlag = "true";
static Connection conn = null;
public TestClob() {
}
public TestClob(String idn, String filename, String flag ) {
id = idn;
fileName = filename;
streamingFlag = flag;
}
public static void main (String args [])
throws SQLException, FileNotFoundException {
TestClob testClob = new TestClob(args[0], args[1], args[2]);
testClob.getConnection();
if (streamingFlag.equals("true")) {
testClob.callUpdateUsingStream(id, fileName);
} else {
testClob.callUpdate(id, fileName);
}
}
public void getConnection() throws SQLException{
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
try {
conn = DriverManager.getConnection
("jdbc:oracle:thin:@incq234a.idc.oracle.com:1521:or9202",
"scott", "tiger");
} catch (SQLException sqlex) {
System.out.println("SQLException while getting db connection: "+sqlex);
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
System.out.println("Exception while getting db connection: "+ex);
if (conn != null) {
conn.close();
}
}
}
public void callUpdate(String id, String filename)
throws SQLException {
CallableStatement cs = null;
CLOB clob = null;
String clobData = null;
try {
String lineSep = System.getProperty("line.separator");
BufferedReader br = new BufferedReader(new FileReader(filename));
String nextLine = "";
StringBuffer sb = new StringBuffer();
while ((nextLine = br.readLine()) != null) {
sb.append(nextLine);
sb.append(lineSep);
}
clobData = sb.toString();
cs = (CallableStatement)
conn.prepareCall( "begin updateStory(?,?); end;" );
clob = getCLOB(clobData);
cs.setObject(1, id );
cs.setObject(2, clob );
cs.execute();
cs.close();
} catch ( SQLException ex ) {
System.out.println("SQLException status : " + ex.getMessage( ) );
} catch ( Exception ex ) {
System.out.println("some exception " + ex.getMessage( ) );
} finally {
try {
if ( cs != null ) {
cs.close( );
}
if ( clob != null ) {
clob.freeTemporary();
}
if (conn !=null) {
conn.close();
}
} catch ( Exception ex ) {
System.out.println("Some exception in callUpdate method of given "
+ "status : " + ex.getMessage( ) );
}
}
}
private CLOB getCLOB( String clobData )
throws Exception {
CLOB tempClob = null;
try {
tempClob = CLOB.createTemporary( conn, true, CLOB.DURATION_SESSION );
tempClob.open( CLOB.MODE_READWRITE );
Writer tempClobWriter = tempClob.getCharacterOutputStream( );
tempClobWriter.write( clobData );
tempClobWriter.flush( );
tempClobWriter.close( );
tempClob.close( );
} catch ( Exception exp ) {
tempClob.freeTemporary();
System.out.println("Exception thrown in getCLOB method "
+ "of given status : "
+ exp.getMessage( ) );
}
return tempClob;
}
public void callUpdateUsingStream(String id, String filename )
throws SQLException, FileNotFoundException {
CallableStatement cs = null;
try {
conn.setAutoCommit(false);
String fileName = filename;
File file = new File(fileName);
FileInputStream fin = new FileInputStream(file);
cs = (CallableStatement)
conn.prepareCall( "begin updateStory(?,?); end;" );
cs.setObject(1, id );
cs.setAsciiStream(2,fin,(int)file.length());
cs.execute();
conn.setAutoCommit(true);
} catch ( SQLException sqlex ) {
System.out.println("SQLException in callUpdateUsingStream method "+
" of given status : " + sqlex.getMessage() );
} catch ( FileNotFoundException fnex ) {
System.out.println("FileNotFoundException in callUpdateUsingStream method "+
" of given status : " + fnex.getMessage( ) );
} finally {
try {
if ( cs != null ) {
cs.close( );
}
if (conn !=null) {
conn.close();
}
} catch ( Exception ex ) {
System.out.println("Some exception in callUpdateUsingStream method "+
" of given status : " + ex.getMessage( ) );
}
}
}
}
|