1 ////////////////////////////////////////////////////////////////////////////////////////////
            2 //
            3 //  @name TestJdbcOdbc.java
            4 //
            5 //  @discription 測試數據庫連接,以及從數據庫中簡單讀取數據
            6 //
            7 //  @author hcm
            8 //
            9 //  @date 2006-12
           10 //
           11 //  [注]:首先建立access數據庫 test ,然后建立數據表 test ,寫入一些測試數據,其次建立數據源test
           12 //
           13 /////////////////////////////////////////////////////////////////////////////////////////////
           14 package TestJdbcOdbc;
           15 
           16 import java.lang.String;
           17 import java.sql.*;
           18 public class TestJdbcOdbc
           19 {
           20     public Connection con;
           21     public  Statement stm;
           22     public ResultSet rs ;
           23     /** Creates a new instance of TestJdbcOdbc */
           24     public TestJdbcOdbc ()
           25     {
           26         
           27     }
           28     //建立連接
           29     public void init()
           30     {
           31           try
           32         {
           33             Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
           34         }
           35         catch(Exception e)
           36         {
           37             System.out.println ("初始化驅動程序異常:"+e.getMessage ());
           38         }
           39         try
           40         {
           41             con = DriverManager.getConnection ("jdbc:odbc:test");
           42             /*參數一:
           43              ResultSet.TYPE_FORWARD_ONLY
           44              ResultSet.TYPE_SCROLL_INSENSITIVE 
           45              或 ResultSet.TYPE_SCROLL_SENSITIVE 
           46              */
           47             /*參數二:
           48              *   ResultSet.CONCUR_READ_ONLY 
           49              或 ResultSet.CONCUR_UPDATABLE 之一 
           50              */
           51             //生成可更新可滾動 狀態集
           52             stm = con.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
           53         }catch(SQLException e)
           54         {
           55             System.out.println ("連接初始化發生異常:"+e.toString ());
           56         }
           57         
           58     }
           59     
           60     //插入數據
           61     public void executeInsert(String s)
           62     {
           63         try
           64         {
           65             int i = stm.executeUpdate(s);
           66         }
           67         catch(SQLException sqlexception)
           68         {
           69             System.err.println("插入數據時發生異常:" + sqlexception.getMessage());
           70         }
           71     }
           72     //刪除數據
           73     public void delData(String sql)
           74     {
           75          try
           76         {
           77           stm.executeUpdate(sql);
           78         }
           79         catch(SQLException e)
           80         {
           81             System.err.println("刪除數據失敗: " + e.getMessage());
           82         }
           83     }
           84     //查詢數據
           85     public ResultSet  queryData ( String sql)
           86     {
           87       try{
           88            rs = stm.executeQuery (sql);               
           89         }
           90         catch(SQLException e)
           91         {
           92             System.out.println ("數據庫連接異常:"+e.getMessage ());
           93             return rs = null;
           94         }
           95         catch(Exception e)
           96         {
           97             System.out.println ("異常:"+e.getMessage ());
           98             return rs = null;
           99         }
          100       return rs;
          101     }
          102     //執行更新
          103       public int UpdateData(String s)
          104     {
          105         int i = 0;
          106         try
          107         {
          108            i = stm.executeUpdate(s);
          109         }
          110         catch(SQLException sqlexception)
          111         {
          112             System.err.println("更新數據庫發生異常: " + sqlexception.getMessage());
          113         }
          114         return i;
          115     }
          116     //關閉連接
          117     public void close()
          118     {
          119         try
          120         {
          121             stm.close();
          122             con.close();
          123         }
          124         catch(SQLException sqlexception)
          125         {
          126             System.err.println("關閉數據庫異常: " + sqlexception.getMessage());
          127         }
          128     }
          129     /**
          130      * @param args the command line arguments
          131      */
          132     public static void main (String[] args)
          133     {
          134         TestJdbcOdbc exec = new TestJdbcOdbc ();
          135         //建立連接
          136         exec.init();
          137         /**********************************************************************************/
          138         //測試查詢
          139         String  querysql = "select * from test";
          140         try
          141         {
          142            exec.rs = exec.queryData (querysql);
          143            while((exec.rs).next ())
          144             {
          145                 System.out.println ("id="+exec.rs.getString ("id")+" name= "+exec.rs.getString ("name")+"  age="+exec.rs.getString ("age"));
          146             }
          147         }catch(SQLException e)
          148         {
          149             System.out.println ("遍歷查詢結果時異常:"+e.toString ());
          150         }
          151         catch(Exception e)
          152         {
          153             System.out.println ("發生:"+e.toString ()+" 異常");
          154         }
          155         
          156         /**********************************************************************************/
          157         /**********************************************************************************/
          158         
          159     //測試刪除數據
          160         String delsql = "delete * from test where id='20031'";
          161         try
          162         {
          163             exec.delData (delsql);
          164         }
          165         catch(Exception e)
          166         {
          167             System.out.println("刪除數據發生:"+e.getMessage ());
          168         }
          169         /**********************************************************************************/
          170         /**********************************************************************************/
          171     //測試插入數據
          172         String insertsql = "insert into test(id,name,age) values ('20031','test111','88')";
          173         try
          174         {
          175             exec.executeInsert (insertsql);
          176         }catch(Exception e)
          177         {
          178             System.out.println("插入數據:"+e.getMessage());
          179         }
          180         /**********************************************************************************/
          181         /**********************************************************************************/
          182         //測試更新數據
          183         String  updatesql = "update test set id = '20200',name='hechangmin' where id ='2002'";
          184        try
          185         {
          186             System.out.println(exec.UpdateData(updatesql));
          187         }catch(Exception e)
          188         {
          189             System.out.println("更新數據發生:"+e.getMessage());
          190         }
          191         /**********************************************************************************/
          192         //關閉連接
          193         exec.close();
          194     }
          195     
          196 }
          197 

          posted on 2007-02-06 17:07 -274°C 閱讀(923) 評論(0)  編輯  收藏 所屬分類: JAVA

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 914173
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 高平市| 仙居县| 夏邑县| 青州市| 永春县| 山东| 冕宁县| 廊坊市| 周宁县| 罗田县| 刚察县| 新晃| 宁德市| 建阳市| 资兴市| 武鸣县| 开封县| 枝江市| 东至县| 金川县| 武汉市| 萍乡市| 微博| 永昌县| 鄄城县| 广灵县| 东城区| 溆浦县| 苏尼特左旗| 朝阳市| 富蕴县| 文山县| 天台县| 和静县| 信丰县| 郸城县| 芦山县| 天气| 广灵县| 江津市| 河东区|