Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Inserting Large XML file into Oracle Database through java

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Inserting Large XML file into Oracle Database through java

    Hi ,

    I am trying to insert a large xml file into oracle database , i am getting the following error ,please help me.

    ORA-30676: socket read or write failed
    ORA-06512: at "SYS.XMLTYPE", line 5


    Following is my code

    import oracle.sql.CLOB;
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.io.Writer;

    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import org.w3c.dom.Document;
    import java.sql.SQLException;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.sql.*;
    import oracle.jdbc.OraclePreparedStatement;
    import oracle.sql.CLOB;
    import oracle.xdb.XMLType;
    public class InsertXml {
    // private static Connection conn=null;
    public static String getXMLString(java.sql.Connection conn, Document doc)throws SQLException

    {

    //String query="<?xml version=\"1.0\"?><customer><name>Joe Smith</name><Job>SoftWare</Job><title>Mathematician</title></customer>";
    String SQLTEXT = "";
    XMLType xml = null;
    xml = XMLType.createXML(conn,doc);
    //System.out.println("xml oject "+xml.toString());

    try {
    //xml = XMLType.createXML(conn,new FileInputStream("C://har1.xml"));
    xml = XMLType.createXML(conn,doc);
    // return xml.getStringVal().trim().replace("null", "").replace("\000", " ").replace("\0x0", " ").replace("\r\n", " ").replace("\n", " ");

    } catch (Exception e) {
    e.printStackTrace();
    }
    return xml.getStringVal();
    //return xml.getStringVal().trim().replace("null", "").replace("\000", " ").replace("\0x0", " ").replace("\r\n", " ").replace("\n", " ");

    }

    private static void insertXML(String xmlData, Connection conn) {
    CLOB clob = null;
    String query;
    //XMLType xml = null;
    //xml = XMLType.createXML(conn,doc);
    // Initialize statement Object
    OraclePreparedStatement pstmt = null;
    try{
    //query="insert into xml_tab(id) values(?)";
    //

    query = "INSERT INTO xml_tab(xmldata) values(xmltype.createxml(?))";

    // Get the statement Object
    pstmt = (OraclePreparedStatement)conn.prepareStatement(que ry);
    // xmlData is the string that contains the XML Data.
    // Get the CLOB object.
    clob = getCLOB(xmlData, conn);
    System.out.println("clob is "+clob);
    // Bind this CLOB with the prepared Statement
    //pstmt.setInt(1,2);
    // pstmt.setString(2,"test.xml");
    pstmt.setObject(1,clob);
    // pstmt.setObject(1, clob);
    // Execute the Prepared Statement
    if (pstmt.executeUpdate()==1) {
    System.out.println ("Successfully inserted a Purchase Order");
    }
    } catch(SQLException sqlexp){
    sqlexp.printStackTrace();
    } catch(Exception exp){
    exp.printStackTrace();
    }
    }

    private static CLOB getCLOB(String xmlData, Connection conn) throws SQLException{
    CLOB tempClob = null;
    try{
    // If the temporary CLOB has not yet been created, create one
    tempClob = CLOB.createTemporary(conn, true, CLOB.DURATION_SESSION);

    // Open the temporary CLOB in readwrite mode, to enable writing
    tempClob.open(CLOB.MODE_READWRITE);
    // Get the output stream to write
    Writer tempClobWriter = tempClob.getCharacterOutputStream();
    // Write the data into the temporary CLOB
    tempClobWriter.write(xmlData);

    // Flush and close the stream
    tempClobWriter.flush();
    tempClobWriter.close();

    // Close the temporary CLOB
    tempClob.close();
    } catch(SQLException sqlexp){
    tempClob.freeTemporary();
    sqlexp.printStackTrace();
    } catch(Exception exp){
    tempClob.freeTemporary();
    exp.printStackTrace();
    }
    return tempClob;
    }

    public static void main(String arg[]) {

    try {
    Connection conn=null;
    conn=DBConnection.getConnection();

    File fXmlFile = new File("C:/har1.xml");
    //String filetxt=fXmlFile.toString();
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    // doc.getDocumentElement().normalize();
    String strxml=getXMLString(conn,doc);
    insertXML(strxml,conn);

    //doInsert(conn, doc);

    } catch (Exception e) {
    e.printStackTrace();
    }
    }




    } Edit Post Reply Reply With Quote .


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Inserting Large XML file into Oracle Database through java

    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inserting Large XML file into Oracle Database through java

    Hi Norm i didn't understand ,what u said

Similar Threads

  1. Java Barcode Program with Oracle database
    By techsing14 in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: December 6th, 2012, 05:19 AM
  2. Inserting Large XML file into Oracle Database through java
    By hari_582 in forum JDBC & Databases
    Replies: 0
    Last Post: April 14th, 2012, 03:19 PM
  3. [SOLVED] Inserting date into database
    By mithcool in forum Java Theory & Questions
    Replies: 2
    Last Post: July 16th, 2011, 01:54 AM
  4. Large Text file(1GB-10GB) Java Swing
    By fortune2k in forum AWT / Java Swing
    Replies: 4
    Last Post: March 27th, 2011, 01:54 PM
  5. java.net.HttpURLConnection:large file to upload
    By tommy_725 in forum Java Networking
    Replies: 1
    Last Post: October 28th, 2009, 11:53 AM