大數據存儲問題的代碼(初級),沒有進行優化的。

          代碼在前天做好了,但是一直都沒時間去修改,
          偷下懶吧。
          呵呵
          放出來只是給大家一點參考。
          我用的表示temp。中間有三個屬性time(存儲系統當前時間),title(文件名稱),context(文件中文本內容),
          其中測試的是在E盤下的test文件夾下的文件,(文件采用問txt的)。
          oracle是10 XE版本的。驅動是class12.
          下面就是代碼了。

          package PreparedTest;

          import java.io.BufferedReader;
          import java.io.BufferedWriter;
          import java.io.File;
          import java.io.FileNotFoundException;
          import java.io.FileReader;
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.ResultSet;
          import java.sql.Statement;
          import java.text.SimpleDateFormat;
          import java.util.Date;

          public class lsat {
           static String driverClass = "oracle.jdbc.driver.OracleDriver";
           static String URL = "jdbc:oracle:thin:@localhost:1521:XE";
           static String USERNAME = "system";
           static String PASSWORD = "duduli";
           static Connection con = null;
           static Statement stmt = null;
           static ResultSet rs = null;
           static String filePathName = null;
           static String fileName = null;
           
           public static String  getFileName(File file){  
                  String filename = "";  
                  if (file != null){  
                          filename = file.getName();  
                          int i = filename.lastIndexOf('.');  
                          if (i > 0 && i < filename.length()-1){  
                                  return filename.substring(0,i);  
                          }  
                  }  
                  return   filename;  
          }
           
           
              public static void clobInsert(String filepath)throws Exception {
               try {
                File file = new File(filepath);
                if (file.isDirectory()) {
                            //System.out.println("文件夾");
                 String[] filelist = file.list();
                 for (int i = 0; i < filelist.length; i++) {
                     
               Date dateNow = new Date();     // 取得當前時間
               SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh"); // 對時間進行格式化
               String dateNowStr = dateFormat.format(dateNow);
               System.out.println(dateNowStr);
               
                  File readfile = new File(filepath + "\\" + filelist[i]);
                     //文件掃描
                  filePathName = readfile.getAbsolutePath();
                  if (!readfile.isDirectory()) {
                   fileName = getFileName(readfile);
                   System.out.print("一個文件!\t");
                      System.out.println("文件名:"+fileName);
                   System.out.println("文件地址"+filePathName);

                  } else if (readfile.isDirectory()) {
                   fileName = getFileName(readfile);
                      System.out.print("一個文件夾!\t");
                      System.out.println("文件夾名:"+fileName);
                   System.out.println("文件夾地址:"+filePathName);
                   clobInsert(filepath + "\\" + filelist[i]);
                   }
                  
                        boolean defaultCommit = con.getAutoCommit();
                         con.setAutoCommit(false);
                         try {

                             /* 插入一個空的CLOB對象 */

                    
                    
                             stmt.executeUpdate("INSERT INTO temp VALUES ('"+dateNowStr+"', '"+filePathName+"',EMPTY_CLOB())");
                             /* 查詢此CLOB對象并鎖定 */
                             ResultSet rs = stmt.executeQuery("SELECT context FROM temp WHERE title='"+filePathName+"' FOR UPDATE");
                 
                             while (rs.next()) {
                                 /* 取出此CLOB對象 */
                                 oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("context");
                                 /* 向CLOB對象中寫入數據 */
                                 BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
                                 BufferedReader in = new BufferedReader(new FileReader(fileName));
                                 int c;
                                 while ((c=in.read())!=-1) {
                                     out.write(c);
                                 }
                                 in.close();
                                 out.close();
                             }
                             /* 正式提交 */
                             con.commit();
                         } catch (Exception ex) {
                             /* 出錯回滾 */
                             con.rollback();
                         }
                         /* 恢復原提交狀態 */
                         con.setAutoCommit(defaultCommit);
                  }
                 }
                
                 /* 設定不自動提交 */
           
            }
                  catch (FileNotFoundException e) {
                  System.out.println("readfile() Exception:" + e.getMessage());
                 }
                }
              public static void main(String[] args) throws Exception
              {
                  /* 裝載驅動,建立數據庫連接 */
                  Class.forName(driverClass);
                  con = DriverManager.getConnection(URL,USERNAME,PASSWORD);
                  stmt = con.createStatement();
                 
             
                 clobInsert("e:/test");

            try {
             if (rs != null) {
              rs.close();
             }
             if (stmt != null) {
              stmt.close();
             }
             if (con != null) {
              con.close();
             }
            } catch (Exception e) {
            }
              }
          }



          寫得很凌亂,大家就勉強看看吧。

          posted on 2008-09-15 21:15 duduli 閱讀(1567) 評論(2)  編輯  收藏 所屬分類: java

          評論

          # re: 大數據存儲問題的代碼(初級),沒有進行優化的。 2008-09-17 21:26 44you

          我google了下,不是oracle.sql.CLOB ,提示,您是不是要找 oracle.sql.BLOB  回復  更多評論   

          # re: 大數據存儲問題的代碼(初級),沒有進行優化的。 2008-09-18 12:07 duduli

          沒錯是clob

            CLOB: 字符大對象Clob 用來存儲單字節的字符數據
            BLOB: 用于存儲二進制數據

          @44you
            回復  更多評論   

          <2008年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導航

          統計

          公告

          welcome to my place.

          常用鏈接

          留言簿(5)

          我參與的團隊

          隨筆分類

          隨筆檔案

          新聞分類

          石頭JAVA擺地攤兒

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          @duduli
          主站蜘蛛池模板: 河北区| 射洪县| 社旗县| 绥芬河市| 南汇区| 镇康县| 梁河县| 米林县| 慈利县| 张家界市| 上栗县| 迁安市| 延安市| 舟山市| 融水| 嵊州市| 尉犁县| 武胜县| 平山县| 嘉鱼县| 乡城县| 江源县| 尤溪县| 辰溪县| 尼木县| 米林县| 互助| 沧州市| 漠河县| 胶州市| 东台市| 海门市| 虹口区| 黑山县| 出国| 冀州市| 军事| 遂宁市| 顺昌县| 乐亭县| 隆昌县|