有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

oracle使用ojdbc14插入CLOB。jar JAVA

下面是我与ojdbc14一起使用的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配合得很好。但是这个(setClob())不适用于ojdbc14。我是否可以使用ojdbc14完成同样的任务。罐子有什么建议吗


共 (1) 个答案

  1. # 1 楼答案

    这段代码适用于odbc14。罐子

    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值