隨筆 - 67  文章 - 79  trackbacks - 0
          <2008年4月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          幫人整db2的xml類型字段導入的sample

          import java.io.*;
          import java.sql.*;

          public class sample {

            

              
          public static void main(String[] args) {
                  
          try {
                      Class.forName(
          "com.ibm.db2.jcc.DB2Driver");
                      String url 
          = "jdbc:db2:HH";
                      
                      
          //DB2用的是系統用戶名和密碼
                      Connection con = DriverManager.getConnection (url,"yoursysusername","password");
                      
          //創建表
                      
          //Statement stmn=con.createStatement();
                      
          //stmn.execute("CREATE TABLE hh(id INT NOT NULL PRIMARY KEY,hh_info XML)");
                      
                      
          //insert 示范
                      String sql = "INSERT INTO hh VALUES(?, ?)";
                      PreparedStatement stmt 
          = con.prepareStatement(sql);
                      stmt.setInt(
          15000);
                      File binFile 
          = new File("e:\\hh\\hh1.xml");
                      InputStream inBin 
          = new FileInputStream(binFile);
                      stmt.setBinaryStream(
          2, inBin, (int) binFile.length());
                      stmt.execute();
                  } 
          catch (ClassNotFoundException e) {
                      
                      e.printStackTrace();
                  } 
          catch (SQLException e) {
                      e.printStackTrace();
                      
                  } 
          catch (FileNotFoundException e) {
                     

                      e.printStackTrace();
                  }  

              }

          }
          db2在我單位的機器上跑得慢如蝸牛 讀取的慢慢再說

          補充
          這里是帶讀寫的
          import java.io.*;
          import java.sql.*;

          public class db2sample {

              
          /**
               * 
          @param args
               
          */
              
          public static void main(String[] args) {
                  
          try {
                      Class.forName(
          "com.ibm.db2.jcc.DB2Driver");
                      String url 
          = "jdbc:db2:HH";

                      
          //DB2用的是系統用戶名和密碼
                      Connection con = DriverManager.getConnection(url, "user","password");
                      
          //創建表
                      
          //Statement stmn=con.createStatement();
                      
          //stmn.execute("CREATE TABLE hh(id INT NOT NULL PRIMARY KEY,hh_info XML)");

                      
          //insert/update 示范
                      
          //String sql = "INSERT INTO hh VALUES(?, ?)";
                      String sql = "update hh set hh_info = ? where id = ? ";
                      PreparedStatement stmt 
          = con.prepareStatement(sql);
                      stmt.setInt(
          25000);
                      File binFile 
          = new File("e:\\hh\\hh1.xml");
                      InputStream inBin 
          = new FileInputStream(binFile);

                      stmt.setBinaryStream(
          1, inBin, (int) binFile.length());
                      stmt.execute();

                      String sql2 
          = "SELECT id, hh_info from hh where id = ?";
                      PreparedStatement stmt2 
          = con.prepareStatement(sql2);
                      stmt2.setInt(
          15000);
                      ResultSet resultSet 
          = stmt2.executeQuery();
                      
          if (resultSet.next()) {

                          String xml 
          = resultSet.getString("hh_info");
                          
          // also possible
                          
          //InputStream inputStream = resultSet.getBinaryStream("hh_info");
                          
          // also possible
                          
          //Reader reader = resultSet.getCharacterStream("hh_info");
                          System.out.println(xml);
                      } 
          else {
                          System.out.println(
          "no record");
                      }
                  } 
          catch (ClassNotFoundException e) {

                      e.printStackTrace();
                  } 
          catch (SQLException e) {
                      e.printStackTrace();

                  } 
          catch (FileNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }

              }

          }


          posted on 2008-04-22 12:57 zarra 閱讀(454) 評論(2)  編輯  收藏

          FeedBack:
          # re: DB2 的xml 字段輸入[未登錄] 2008-04-22 13:56 apple
          太感謝了!有老師的幫忙,我的畢設就是小case了!
          讀取以后再說吧!不做也沒關系.我在寫論文了,已經寫了50多頁了,工作量應該已經到了畢設的要求了.  回復  更多評論
            
          # re: DB2 的xml 字段輸入[未登錄] 2008-05-02 19:56 apple
          老師您太好了!幫了我這么多  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 天门市| 玉山县| 探索| 于田县| 吉安县| 翼城县| 镇坪县| 长沙县| 班玛县| 三河市| 炎陵县| 西昌市| 乌苏市| 承德市| 缙云县| 登封市| 鄱阳县| 鹤山市| 鱼台县| 鹤峰县| 宁津县| 上犹县| 枣强县| 左贡县| 麻江县| 光山县| 西青区| 富蕴县| 九江市| 健康| 罗源县| 娱乐| 平顶山市| 五寨县| 登封市| 和硕县| 容城县| 方山县| 弋阳县| 丹巴县| 泽普县|