一種向數(shù)據(jù)庫中寫入數(shù)據(jù)的方法

          今天在我的博客群中有個(gè)朋友問了我一個(gè)問題,詳見http://group.bokee.com/group/subject.7861.11763421854970771.html?91391218870400970

          他問:怎樣把文本文件中的每一列寫到數(shù)據(jù)庫表中?
          我的解決方案如下:
          開發(fā)工具:Eclipse3.2   JDK1.6   MySQL4.1.8

          首先我打開MySQL命令行窗口,輸入密碼root后執(zhí)行如下語句建立測試表student;

          mysql>use test;
          mysql
          >create table student(id int,name varchar(10));



          我的文本文件(E:\data.txt,E盤為我的Java程序所在的根目錄)的格式如下:

          ------------------------

          1,Tom
          2,Kate
          3,Jim
          -----------------------

          對了!還有提醒大家一句:一定要把數(shù)據(jù)庫驅(qū)動(dòng)程序添加到Eclipse工程的JavaBuildpath中,呵呵,對于有經(jīng)驗(yàn)的程序員來講,我這純屬是廢話,好了,不廢話了,看看我的Java程序吧!

           

          *@author 我為J狂 建立日期 2007-4-14
           
          *
           
          */
          package net.blogjava.lzqdiy;

          import java.io.BufferedReader;
          import java.io.File;
          import java.io.FileReader;
          import java.io.IOException;
          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.SQLException;
          import java.sql.Statement;
          import java.util.HashSet;
          import java.util.Set;

          public class Test
          {

              
          /**
               * 
          @param args
               
          */

              
          public static Set<String> readFile(String filePath)
              
          {
                  BufferedReader in 
          = null;
                  Set
          <String> tableData = new HashSet<String>();
                  
          try
                  
          {
                      in 
          = new BufferedReader(new FileReader(filePath));
                      String record 
          = null;
                      
          while ((record = in.readLine()) != null)
                      
          {
                          tableData.add(record);
                      }

                  }
           catch (IOException e)
                  
          {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
           finally
                  
          {
                      
          try
                      
          {
                          in.close();
                      }
           catch (IOException e)
                      
          {
                          
          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }

                  }

                  
          return tableData;
              }


              
          public static void insertDataBase(Set<String> tableData,String driverName,String url,String username,String password)
              
          {
                  Connection con 
          = null;
                  Statement stmt 
          = null;
                  
          try
                  
          {
                      Class.forName(driverName);
                      con 
          = DriverManager.getConnection(
                              url, username, password);
                      stmt 
          = con.createStatement();
                      
          for (String record : tableData)
                      
          {
                          
          if(record!=null)
                          
          {
                              String[] values 
          = record.split(",");
                              stmt.executeUpdate(
          "insert into student(id,name) values(" + values[0]+ ",'" + values[1+ "')");
                          }

                      }


                  }
           catch (ClassNotFoundException e)
                  
          {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
           catch (SQLException e)
                  
          {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
           finally
                  
          {
                      
          if (stmt != null)
                      
          {
                          
          try
                          
          {
                              stmt.close();
                          }
           catch (SQLException e)
                          
          {
                              
          // TODO Auto-generated catch block
                              e.printStackTrace();
                          }

                          stmt 
          = null;
                      }


                      
          if (con != null)
                      
          {
                          
          try
                          
          {
                              con.close();
                          }
           catch (SQLException e)
                          
          {
                              
          // TODO Auto-generated catch block
                              e.printStackTrace();
                          }

                          con 
          = null;
                      }

                  }

              }


              
          public static void main(String[] args)
              
          {
                  
          // TODO Auto-generated method stub
                  
                  insertDataBase(readFile(File.separator
          +"data.txt"),"com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/test""root""root");
              }

          }


           

           我的這個(gè)程序只是能實(shí)現(xiàn)功能,在擴(kuò)展性方面做得還不夠,希望大家多提寶貴意見,我不勝感激。
          另外,我感覺這種數(shù)據(jù)庫輸入數(shù)據(jù)的策略有利有弊,它的優(yōu)點(diǎn)是快速方便,當(dāng)記錄內(nèi)容的差別不大時(shí),可以使用復(fù)制粘貼的方法快速更改文本文件的內(nèi)容,然后執(zhí)行Java程序一切都OK了;它的缺點(diǎn)也很明顯就是靈活性較差,文本文件的中的每行記錄中的數(shù)據(jù)必須用統(tǒng)一的分隔符來劃分每個(gè)字段值(在我文本文件中用的是","),一旦用戶將分隔符寫錯(cuò),那么Java程序就會(huì)失效。



          posted on 2007-04-14 10:21 我為J狂 閱讀(2200) 評論(10)  編輯  收藏 所屬分類: 數(shù)據(jù)庫編程

          評論

          # re: 一種向數(shù)據(jù)庫中寫入數(shù)據(jù)的新方法[未登錄] 2007-04-14 14:23 ibmsoft

          何必這么復(fù)雜呢,souceforge上有個(gè)opencvs  回復(fù)  更多評論   

          # re: 一種向數(shù)據(jù)庫中寫入數(shù)據(jù)的新方法 2007-04-14 17:30 我為J狂

          @ibmsoft
          謝謝您的留言,能不能留個(gè)具體的鏈接地址?我好去學(xué)習(xí)學(xué)習(xí)。
            回復(fù)  更多評論   

          # re: 一種向數(shù)據(jù)庫中寫入數(shù)據(jù)的新方法 2007-04-14 18:07 BeanSoft

          opencvs.souceforge.net 或者 google 一下, 呵呵  回復(fù)  更多評論   

          # re: 一種向數(shù)據(jù)庫中寫入數(shù)據(jù)的新方法 2007-04-14 23:23 Astamei

          Mysql 的Query Browser 中有一個(gè)正值的工具 比這個(gè)方便多了

            回復(fù)  更多評論   

          # re: 一種向數(shù)據(jù)庫中寫入數(shù)據(jù)的新方法 2007-04-15 10:51 我為J狂

          @Astamei
          如果我的數(shù)據(jù)庫是mysql以外的其他數(shù)據(jù)庫怎么辦?  回復(fù)  更多評論   

          # re: 一種向數(shù)據(jù)庫中寫入數(shù)據(jù)的新方法 2007-04-15 17:33 劉甘泉

          這種方法我也用過,項(xiàng)目里面有需要做的
          感覺用preparedstatement要好  回復(fù)  更多評論   

          # re: 一種向數(shù)據(jù)庫中寫入數(shù)據(jù)的新方法 2007-04-15 18:04 我為J狂

          @劉甘泉
          謝謝您的建議,createStatement()返回的是Statement接口,prepareStatement()返回的是PreparedStatement接口,后者繼承自前者,由于PreparedStatement使用了precompile,因而執(zhí)行sql語句更高效,所謂的高效就是在sql語句解析執(zhí)行時(shí),速度更快,所以建議使用prepareStatement()。
            回復(fù)  更多評論   

          # re: 一種向數(shù)據(jù)庫中寫入數(shù)據(jù)的新方法 2007-04-16 18:04 Fadesky

          Mysql不支持從文件導(dǎo)入數(shù)據(jù)嗎?  回復(fù)  更多評論   

          # re: 一種向數(shù)據(jù)庫中寫入數(shù)據(jù)的新方法 2007-04-16 18:24 我為J狂

          @Fadesky
          我是想用程序?qū)霐?shù)據(jù),而不是手工導(dǎo)入數(shù)據(jù),SQLserver和Oracle都支持手工導(dǎo)入,對于MySQL,我不太清楚。  回復(fù)  更多評論   

          # re: 一種向數(shù)據(jù)庫中寫入數(shù)據(jù)的方法 2008-11-29 15:09 哈特.比爾波

          看了一下,寫的很有條例,對我們初學(xué)者來說,很適合。  回復(fù)  更多評論   

          <2007年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(11)

          隨筆分類(48)

          文章分類(29)

          常去逛逛

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 肃宁县| 双江| 绵竹市| 治多县| 凤庆县| 成武县| 晋宁县| 镇沅| 泗水县| 礼泉县| 凤庆县| 合江县| 永顺县| 镇安县| 白水县| 屯门区| 革吉县| 奇台县| 崇阳县| 华安县| 深泽县| 兴宁市| 凌海市| 宣威市| 遂川县| 正宁县| 莱芜市| 原平市| 舞阳县| 和田县| 镇江市| 长沙县| 青铜峡市| 清丰县| 横山县| 杂多县| 阿勒泰市| 丹东市| 桦甸市| 扎鲁特旗| 福建省|