Reputation: 5777
Below is my Java code used with ojdbc14.jar.
OraclePreparedStatement stAddNews;
String queryAddNews = "INSERT INTO CMS_NEWS_ITEMS (CNI_TITLE, CNI_SHORTTITLE, CNI_DATE, CNI_CONTENT, CNI_CREATEDBY, CNI_CREATEDFROM, CNI_ONMOBILE, CNI_ONSLIDER, CNI_CREATEDON, CNI_STATUS, CNI_ID, CNI_IMAGEID) VALUES (?,?,?,?,?,?,?,?,SYSDATE,'ACTIVE','"+newsItemId+"','"+newsItemId+"')";
System.out.println(queryAddNews);
stAddNews = (OraclePreparedStatement) con.prepareStatement(queryAddNews);
Clob myClob = con.createClob();
myClob.setString(1,replaceImgNewsCont);
stAddNews.setString(1, itemTitle);
stAddNews.setString(2, shortTitle);
stAddNews.setString(3, date);
stAddNews.setClob(4,myClob);
stAddNews.executeUpdate();
stAddNews.clearParameters();
stAddNews.close();
con.close();
The above code works well with the ojdbc6.jar but this (setClob()) does't work with ojdbc14. Is there anyway i could accomplish the same task using ojdbc14.jar. Any suggestions?
Upvotes: 1
Views: 2659
Reputation: 71
The answer above was helpful, but I still ran into other problems after I got the concept that the answer was sharing. So I would like to share my solutions:
To Insert a new row with CLOB:
conn.setAutoCommit(false);
PreparedStatement pStmt;
// Use the empty_clob() function to set an empty CLOB value to the column that you want to update,
// to avoid the null pointer exception afterward.
String sql = "UPDATE <TABLE_NAME> SET <COLUMN_NAME>=empty_clob() ";
sql += "<OTHER CONDITIONS>";
pStmt = conn.prepareStatement(sql);
pStmt.executeUpdate(sql);
// use SELECT ... FOR UPDATE to retrieve the target row,
// to get the privilege to write the row.
String sql = "SELECT <COLUMN_NAME> FROM <TABLE_NAME> ";
sql += "<OTHER CONDITIONS> FOR UPDATE";
pStmt = conn.prepareStatement(sql);
ResultSet rs = pStmt.executeQuery();
if (rs.next()) {
oracle.sql.CLOB clob = ((oracle.jdbc.OracleResultSet)rs).getCLOB("<COLUMN_NAME>");
// if the value of the column is null or an empty string, you'll get a null pointer exception on the next line.
java.io.Writer w = clob.getCharacterOutputStream();
w.write(data.toString().toCharArray());
w.flush();
// it might cause an "ORA-22920 row containing the LOB value is not locked" error if you didn't use the SELECT ... FOR UPDATE statement.
w.close();
}
conn.setAutoCommit(true);
conn.commit();
Futhermore, to update a row with CLOB:
conn.setAutoCommit(false);
PreparedStatement pStmt;
// insert a row, with the values that you want to set,
// and set the CLOB column with white space, or use the empty_clob() function
// to make sure the value of the column won't be null or an empty string.
String sql = "INSERT INTO <TABLE_NAME> SELECT <VALUE_1>, ... , <VALUE_N>, ' ' FROM DUAL";
pStmt = conn.prepareStatement(sql);
pStmt.executeUpdate(sql);
// retrieve the row we just insert.
// since the update hasn't been committed yet,
// so we already got the privilege to write the row,
// it's not necessary to use the SELECT...FOR UPDATE statement at here.
sql = "SELECT <COLUMN_NAME> FROM <TABLE_NAME> ";
sql += "<OTHER CONDITIONS>";
pStmt = conn.prepareStatement(sql);
ResultSet rs = pStmt.executeQuery();
if (rs.next()) {
oracle.sql.CLOB clob = ((oracle.jdbc.OracleResultSet)rs).getCLOB("<COLUMN_NAME>");
java.io.Writer w = clob.getCharacterOutputStream();
w.write(data.toString().toCharArray());
w.flush();
w.close();
}
conn.setAutoCommit(true);
conn.commit();
Upvotes: 0
Reputation: 5777
This piece of code works with odbc14.jar.
String strObjID="OBJ00015";
String strContent="asdasdasdas<asdasd>adasdasdasdsadsad";
PreparedStatement pstmt = con.prepareStatement("INSERT INTO TMP_FILE2(ID,FILECONTENT) values(?,EMPTY_CLOB())");
pstmt.setString(1,strObjID);
//pstmt.setClob(2, clob);
pstmt.executeUpdate();
pstmt.close();
//updating CLOB column with String value
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String query="Select FILECONTENT FROM TMP_FILE2 where ID=0001 FOR UPDATE";
con.setAutoCommit(false);
ResultSet resultset=stmt.executeQuery(query);
if(resultset.next()){
oracle.sql.CLOB clobnew = ((oracle.jdbc.OracleResultSet) resultset).getCLOB("FILECONTENT");
byte[] bytes = strContent.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
InputStreamReader isr = new InputStreamReader(bais);
PrintWriter pw = new PrintWriter(clobnew.getCharacterOutputStream() );
BufferedReader br = new BufferedReader(isr);
//new FileReader( new File("D:\\test.txt") ) );
String lineIn = null;
while( ( lineIn = br.readLine() ) != null )
pw.println( lineIn );
pw.close();
br.close();
}
con.setAutoCommit(true);
con.commit();
First You need to INSERT a empty CLOB and then UPDATE the CLOB Value in the specific column.
Upvotes: 1