Java: How to insert CLOB into oracle database

前端 未结 10 1261
情话喂你
情话喂你 2020-12-03 03:32

I need to write an XML file content into oracle database where the column is of CLOB datatype. How will I do that?

10条回答
  •  死守一世寂寞
    2020-12-03 03:56

    For this purpose you need to make the connection result set

    ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE

    Connection con=null;
    //initialize connection variable to connect to your database...
    Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    String query="Select MYCLOB from TABLE_NAME for update";
    con.setAutoCommit(false);
    ResultSet resultset=stmt.executeQuery(query);
    
    
    if(resultset.next()){
    oracle.sql.CLOB    clobnew = ((OracleResultSet) rss).getCLOB("MYCLOB");
    PrintWriter pw = new PrintWriter(clobnew.getCharacterOutputStream() );
    BufferedReader br = new BufferedReader( new FileReader( new File("filename.xml") ) );
    String  lineIn = null;
    while( ( lineIn = br.readLine() ) != null )
          pw.println( lineIn );
          pw.close();
          br.close();
    }
    
    con.setAutoCommit(true);
    con.commit();
    }
    

    Note: its important that you add the phrase for update at the end of the query that is written to select the row...

    Follow the above code to insert the XML file

提交回复
热议问题