Java: How to insert CLOB into oracle database

前端 未结 10 1135
情话喂你
情话喂你 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:52

    Take a look at the LobBasicSample for an example to use CLOB, BLOB, NLOB datatypes.

    0 讨论(0)
  • 2020-12-03 03:54

    I had similar issue. Changed one of my table column from varchar2 to CLOB. I didn't needed to change any java code. I kept it as setString(..) only so no need to change set method as setClob() etch if you are using following versions ATLEAST of Oracle and jdbc driver.

    I tried in In Oracle 11g and driver ojdbc6-11.2.0.4.jar

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-03 03:59

    The easiest way is to simply use the

    stmt.setString(position, xml);
    

    methods (for "small" strings which can be easily kept in Java memory), or

    try {
      java.sql.Clob clob = 
        oracle.sql.CLOB.createTemporary(
          connection, false, oracle.sql.CLOB.DURATION_SESSION);
    
      clob.setString(1, xml);
      stmt.setClob(position, clob);
      stmt.execute();
    }
    
    // Important!
    finally {
      clob.free();
    }
    
    0 讨论(0)
  • 2020-12-03 04:00

    This code worked for me. I use ojdbc6-11.2.0.2.jar.

    java.sql.Connection con;
    javax.xml.bind.Marshaller marshaller;
    
    Clob xmlClob = con.createClob();
    try {
      try (Writer xmlClobWriter = xmlClob.setCharacterStream(1)) {
        m.marshal(jaxbObject, xmlClobWriter);
      } // xmlClobWriter.close();
      try (PreparedStatement stmt = con.prepareStatement("INSERT INTO table (xml) values(?)")) {
        stmt.setClob(1, xmlClob);
        stmt.executeUpdate();
      }
    } finally {
      xmlClob.free();
    }
    
    0 讨论(0)
  • 2020-12-03 04:01

    Converting clob to string:

    Clob clob=rs.getClob(2);      
    String str=(String)clob.getSubString(1,(int)clob.length());      
    System.out.println("Clob Data is : "+str);
    
    0 讨论(0)
提交回复
热议问题