Java writes xml to Oracle xmlType

Source: Internet
Author: User

Import java. io. BufferedReader;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. InputStream;
Import java. io. InputStreamReader;
Import java. io. OutputStream;
Import java. io. Reader;
Import java. io. Writer;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;
Import java. util. ArrayList;
Import java. util. List;

Import Oracle. SQL. BLOB;
Import oracle. SQL. CLOB;

Public class TestXMLType {

/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Connection conn = null;
Try
{
// Need input the name of manager
// Add the picture's path
String strName = "test ";
String strPhoto = "C:/test. xml ";
Class. forName ("oracle. jdbc. driver. OracleDriver"). newInstance ();

String str = "jdbc: oracle: thin: @ 127.0.0.1: 1521: orcl ";

Conn = DriverManager. getConnection (str, "scott", "tigger ");


InputStream ins = null;
StringBuffer buffer = new StringBuffer ();
Ins = new FileInputStream (new File ("C:/test. xml "));

Byte [] tmp = new byte [10];
Int length = 0;
While (length = ins. read (tmp ))! =-1)
{
Buffer. append (new String (tmp, 0, length ));
}

Ins. close ();
InsertXML (buffer. toString (), conn );

}
Catch (Exception ex)
{
Ex. printStackTrace ();
}
Finally
{
If (conn! = Null)
{
Try {
Conn. close ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
}

Private static void insertXML (String xmlData, Connection conn ){
CLOB clob = null;
String query;
// Initialize statement Object
PreparedStatement pstmt = null;
Try {
Query = "insert into zxx_xml VALUES (?, XMLType (?)) ";
// Get the statement Object
Pstmt = conn. prepareStatement (query );

// XmlData is the string that contains the XML Data.
// Get the CLOB object using the getCLOB method.
Clob = getCLOB (xmlData, conn );
// Bind this CLOB with the prepared Statement
Pstmt. setString (1, "F000002LNW ");
Pstmt. setObject (2, clob );
// Execute the Prepared Statement
If (pstmt.exe cuteUpdate () = 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;
}

 

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.