Java: How to insert CLOB into oracle database

2019-01-14 03:22发布

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

10条回答
放我归山
2楼-- · 2019-01-14 03:46

With about 100 lines of code ;-) Here is an example.

The main point: Unlike with other JDBC drivers, the one from Oracle doesn't support using Reader and InputStream as parameters of an INSERT. Instead, you must SELECT the CLOB column FOR UPDATE and then write into the ResultSet

I suggest that you move this code into a helper method/class. Otherwise, it will pollute the rest of your code.

查看更多
\"骚年 ilove
3楼-- · 2019-01-14 03:46

You can very well do it with below code, i am giving you just the code to insert xml hope u are done with rest of other things..

import oracle.xdb.XMLType;

//now inside the class......
// this will be to convert xml into string

File file = new File(your file path);
FileReader fileR = new FileReader(file);
fileR.read(data);
String str = new String(data);

// now to enter it into db

conn = DriverManager.getConnection(serverName, userId, password);

XMLType objXml = XMLType.createXML(conn, str);

// inside the query statement put this code

objPreparedstatmnt.setObject(your value index, objXml);

I have done like this and it is working fine.

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-14 03:51

Try this , there is no need to set its a CLOB

  public static void main(String[] args)
        {
        try{    

                    System.out.println("Opening db");

                    Class.forName("oracle.jdbc.driver.OracleDriver"); 
                    if(con==null)
                     con=DriverManager.getConnection("jdbc:oracle:thin:@192.9.200.103:1521: orcl","sas","sas");  
                    if(stmt==null)
                    stmt=con.createStatement();  


                    int res=9;

                    String usersSql = "{call Esme_Insertsmscdata(?,?,?,?,?)}";
                    CallableStatement stmt = con.prepareCall(usersSql);
            // THIS THE CLOB DATA  
            stmt.setString(1,"SS¶5268771¶00058711¶04192018¶SS¶5268771¶00058712¶04192018¶SS¶5268772¶00058713¶04192018¶SS¶5268772¶00058714¶04192018¶SS¶5268773¶00058715¶04192018¶SS¶5268773¶00058716¶04192018¶SS¶5268774¶00058717¶04192018¶SS¶5268774¶00058718¶04192018¶SS¶5268775¶00058719¶04192018¶SS¶5268775¶00058720¶04192018¶");     
                    stmt.setString(2, "bcvbcvb");
                    stmt.setString(3, String.valueOf("4522"));
                    stmt.setString(4, "42.25.632.25");
                    stmt.registerOutParameter(5,OracleTypes.NUMBER);    
                    stmt.execute();
                    res=stmt.getInt(5);
                    stmt.close();

                    System.out.println(res);




                    }
                    catch(Exception e)
                    { 

                         try 
                         {
                            con.close();
                        } catch (SQLException e1) {


                        }
                    }  
                }
    }
查看更多
虎瘦雄心在
5楼-- · 2019-01-14 03:53

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();
}
查看更多
▲ chillily
6楼-- · 2019-01-14 03:59

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();
}
查看更多
等我变得足够好
7楼-- · 2019-01-14 04:01

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

查看更多
登录 后发表回答