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:
- Create table xmltable(id int , content xml);
2. Execute the following code to save the XML file on drive C to the database.
- Package X2R2D;
- Import java. io .*;
- Import java. SQL .*;
- Public class xml2db2
- {
- Private Connection con = null;
- Private PreparedStatement pstat = 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: // XXX ";
Database Name
String user = "*******"; // user Name
String password = "*******"; // password
- con=DriverManager.getConnection(url, user, password);
- return true;
- }
- catch(SQLException e)
- {
- e.printStackTrace();
- return false;
- }
- }
- public void getInsert() throws Exception
- {
- pstat= con.prepareStatement("INSERT INTO xmltable VALUES (?,?)");
- String xmlfile="c:/1.xml";
- String xmlString = "";
Read XML data from the file and construct the data of the String type. try {
- InputStreamReader isr = new InputStreamReader(new FileInputStream(xmlfile), "UTF-8");
Attention Encoding
- BufferedReader in = new BufferedReader (isr );
- String line = null;
- While (line = in. readLine ())! = Null)
- {
- XmlString + = line;
- }
- In. close ();
- Pstat. setInt (1, 2 );
- Pstat. setString (2, xmlString );
- Pstat.exe cuteUpdate ();
- }
- Public boolean closeConn () throws SQLException
- {
- Con. close ();
- Return true;
- }
- Public static void main (String [] args) throws Exception
- {
- Xml2db2 db = new xml2db2 ();
- Db. openConn ();
- Db. getInsert ();
- Db. closeConn ();
- System. out. println ("XML successfully written to DB2 database ");
- }
- }
The above content is an introduction to the Successful Saving of java XML into DB2 code. I hope you will get something.