Steps for successfully storing java XML into DB2 code

Source: Internet
Author: User

The actual operation procedure for successfully storing java XML into DB2 code. If you have successfully stored java XML into DB2 code in actual operations, however, if you do not know how to operate on it correctly, the following articles must be a mentor to you.

1. First, create a table on DB2:

 
 
  1. Create table xmltable(id int , content xml); 

2. Execute the following code to save the XML file on drive C to the database.

 
 
  1. Package X2R2D;
  2. Import java. io .*;
  3. Import java. SQL .*;
  4. Public class xml2db2
  5. {
  6. Private Connection con = null;
  7. Private PreparedStatement pstat = null;
  8. Public boolean openConn () throws Exception
  9. {
  10. Try {
  11. // Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
  12. Try {Class. forName ("com. ibm. db2.jcc. DB2Driver");} catch (Exception e) {System. out. println ("driver failed ");}
  13. String url = "jdbc: db2: // XXX ";

Database Name

String user = "*******"; // user Name

String password = "*******"; // password

 
 
  1. con=DriverManager.getConnection(url, user, password);  
  2. return true;  
  3. }  
  4. catch(SQLException e)  
  5. {  
  6. e.printStackTrace();  
  7. return false;  
  8. }   
  9. }  
  10. public void getInsert() throws Exception  
  11. {  
  12. pstat= con.prepareStatement("INSERT INTO xmltable VALUES (?,?)");  
  13. String xmlfile="c:/1.xml";  
  14. String xmlString = ""; 

Read XML data from the file and construct the data of the String type. try {

 
 
  1. InputStreamReader isr = new InputStreamReader(new FileInputStream(xmlfile), "UTF-8");  

Attention Encoding

 
 
  1. BufferedReader in = new BufferedReader (isr );
  2. String line = null;
  3. While (line = in. readLine ())! = Null)
  4. {
  5. XmlString + = line;
  6. }
  7. In. close ();
  8. Pstat. setInt (1, 2 );
  9. Pstat. setString (2, xmlString );
  10. Pstat.exe cuteUpdate ();
  11. }
  12. Public boolean closeConn () throws SQLException
  13. {
  14. Con. close ();
  15. Return true;
  16. }
  17. Public static void main (String [] args) throws Exception
  18. {
  19. Xml2db2 db = new xml2db2 ();
  20. Db. openConn ();
  21. Db. getInsert ();
  22. Db. closeConn ();
  23. System. out. println ("XML successfully written to DB2 database ");
  24. }
  25. }

The above content is an introduction to the Successful Saving of java XML into DB2 code. I hope you will get something.

Related Article

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.