java XML成功存入DB2 代碼的實(shí)際操作步驟
java XML成功存入DB2 代碼的實(shí)際操作步驟,假如你在實(shí)際操作中java XML成功存入DB2 代碼的實(shí)際操作,但是你卻不知道對(duì)其如何正確的對(duì)其進(jìn)行操作的話,那么以下的文章對(duì)你而言一定是良師益友。
1、首先現(xiàn)在DB2上建立表:
- Create table xmltable(id int , content xml);
2、執(zhí)行下面代碼把c盤下的XML文件存入數(shù)據(jù)庫
- 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("驅(qū)動(dòng)失敗");}
- String url="jdbc:db2://××××××";
數(shù)據(jù)庫名
String user="******"; //用戶名
String 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 = "";
從文件中讀取XML數(shù)據(jù),構(gòu)造成String類型的數(shù)據(jù)try{
- InputStreamReader isr = new InputStreamReader(new FileInputStream(xmlfile), "UTF-8");
注意編碼
- 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.executeUpdate();
- }
- 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成功寫入DB2數(shù)據(jù)庫");
- }
- }
以上的相關(guān)內(nèi)容就是對(duì)java XML成功存入DB2 代碼的介紹,望你能有所收獲。
【編輯推薦】