以下是我与ojdbc14.jar一起使用的Java代码。
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();
上面的代码适用于ojdbc6.jar,但是这个(setClob())不适用于ojdbc14。无论如何,我可以使用ojdbc14.jar完成相同的任务。有什么建议吗?
答案 0 :(得分:1)
这段代码适用于ojdbc14.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();
首先,您需要INSERT一个空的CLOB,然后更新特定列中的CLOB值。
答案 1 :(得分:0)
上面的答案很有帮助,但是在我得到答案共享的概念后,我仍然遇到其他问题。因此,我想分享我的解决方案:
要使用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,使用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();