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)
- Package X2R2D;
- Import java. SQL. Connection;
- Import java. SQL. DriverManager;
- Import java. SQL. ResultSet;
- Import java. SQL. SQLException;
- Import java. SQL. Statement;
- Public class DataBase
- {
- Private Connection conn = null;
- Private Statement stmt = null;
-
- Public boolean openConn () throws Exception
- {
- Try {
- // Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
- Try {Class. forName ("com. ibm. db2.jcc. DB2Driver");} catch (Exception e) {System. out. println ("driver failed ");}
- String url = "jdbc: db2 ://************";
- String user = "************";
- String password = "************";
- Conn = DriverManager. getConnection (url, user, password );
- Return true;
- }
- Catch (SQLException e)
- {
- E. printStackTrace ();
- Return false;
- }
-
- }
- Public boolean openStatement () throws SQLException
- {
- Stmt = conn. createStatement ();
- // Stmt = conn. createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
- Return true;
- }
- Public ResultSet getQuery (String SQL) throws Exception
- {
- ResultSet rs = null;
- Rs1_stmt.exe cuteQuery (SQL );
- Return rs;
- }
- Public boolean closeConn () throws SQLException
- {
- Conn. close ();
- Return true;
- }
- Public static void main (String [] args) throws Exception
- {
- ResultSet rs = null;
- Int I = 1;
- String SQL = null;
- DataBase db = new DataBase ();
- Db. openConn ();
- Db. openStatement ();
Query
- String a="select id, XMLQUERY('for $e in $d/root/book return $e/name/text()' passing d.content as \"d\" )";
- String b=" from xmltable d ";
- String c=" where id=2";
- sql=a+b+c;
- rs=db.getQuery(sql);
- while(rs.next())
- {
- //System.out.println("id:"+rs.getInt("son")+" name:"+rs.getString("NAME"));
- System.out.println("id:"+rs.getInt(1)+" name:"+rs.getString(2));
- }
- db.closeConn();
- }
- }
The above content describes the successful reading of the XML code from java DB2, hoping to help you in this regard.