Steps for reading XML code in java DB2

Source: Internet
Author: User

The following articles mainly introduce the correct steps for reading XML code from java DB2. If you are interested in the correct steps for reading XML code from java DB2, you can click to view the following articles. The following is a detailed description of the main content of the article. I hope you will have a better understanding of it after browsing.

If you are interested, refer to the usage of Xquery/SQL on the IBM website I quoted. Here I will give a simple but practical example: the code has been verified)

 
 
  1. Package X2R2D;
  2. Import java. SQL. Connection;
  3. Import java. SQL. DriverManager;
  4. Import java. SQL. ResultSet;
  5. Import java. SQL. SQLException;
  6. Import java. SQL. Statement;
  7. Public class DataBase
  8. {
  9. Private Connection conn = null;
  10. Private Statement stmt = null;
  11.  
  12. Public boolean openConn () throws Exception
  13. {
  14. Try {
  15. // Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
  16. Try {Class. forName ("com. ibm. db2.jcc. DB2Driver");} catch (Exception e) {System. out. println ("driver failed ");}
  17. String url = "jdbc: db2 ://************";
  18. String user = "************";
  19. String password = "************";
  20. Conn = DriverManager. getConnection (url, user, password );
  21. Return true;
  22. }
  23. Catch (SQLException e)
  24. {
  25. E. printStackTrace ();
  26. Return false;
  27. }
  28.  
  29. }
  30. Public boolean openStatement () throws SQLException
  31. {
  32. Stmt = conn. createStatement ();
  33. // Stmt = conn. createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
  34. Return true;
  35. }
  36. Public ResultSet getQuery (String SQL) throws Exception
  37. {
  38. ResultSet rs = null;
  39. Rs1_stmt.exe cuteQuery (SQL );
  40. Return rs;
  41. }
  42. Public boolean closeConn () throws SQLException
  43. {
  44. Conn. close ();
  45. Return true;
  46. }
  47. Public static void main (String [] args) throws Exception
  48. {
  49. ResultSet rs = null;
  50. Int I = 1;
  51. String SQL = null;
  52. DataBase db = new DataBase ();
  53. Db. openConn ();
  54. Db. openStatement ();

Query
 

 
 
  1. String a="select id, XMLQUERY('for $e in $d/root/book return $e/name/text()' passing d.content as \"d\" )";  
  2. String b=" from xmltable d ";  
  3. String c=" where id=2";  
  4. sql=a+b+c;  
  5. rs=db.getQuery(sql);  
  6. while(rs.next())  
  7. {  
  8. //System.out.println("id:"+rs.getInt("son")+" name:"+rs.getString("NAME"));  
  9. System.out.println("id:"+rs.getInt(1)+" name:"+rs.getString(2));  
  10. }   
  11. db.closeConn();  
  12. }  
  13. }  

The above content describes the successful reading of the XML code from java DB2, hoping to help you in this regard.

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.