1

下面是我与 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 完成相同的任务。有什么建议么?

4

2 回答 2

1

这段代码适用于 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();

首先,您需要插入一个空的 CLOB,然后更新特定列中的 CLOB 值。

于 2013-09-20T10:53:23.890 回答
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();

此外,要使用 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();
于 2020-09-03T07:23:44.383 回答