<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

      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 閱讀(924) 評論(0)  編輯  收藏 所屬分類: JAVA

    常用鏈接

    留言簿(21)

    隨筆分類(265)

    隨筆檔案(242)

    相冊

    JAVA網站

    關注的Blog

    搜索

    •  

    積分與排名

    • 積分 - 914354
    • 排名 - 40

    最新評論

    主站蜘蛛池模板: 91九色精品国产免费| 一级毛片免费播放视频| 亚洲一区动漫卡通在线播放| 中文字幕亚洲综合精品一区| 亚洲国产精品一区| 久久狠狠高潮亚洲精品| 亚洲视频一区二区在线观看| 亚洲精品免费在线| 亚洲中文字幕久久精品无码2021| 亚洲永久中文字幕在线| 亚洲一卡二卡三卡| 亚洲中文无码永久免| 精品国产日韩亚洲一区在线| 国产亚洲精品2021自在线| kk4kk免费视频毛片| 爽爽爽爽爽爽爽成人免费观看| 日本卡1卡2卡三卡免费| 久久永久免费人妻精品下载| 久久www免费人成看片| 手机在线毛片免费播放| 国产18禁黄网站免费观看| 亚洲乱码国产一区网址| 亚洲成亚洲乱码一二三四区软件| 亚洲欧洲第一a在线观看| 亚洲成aⅴ人片在线影院八| 亚洲中文字幕久久无码| 美女裸免费观看网站| 中文字幕免费人成乱码中国| 久热免费在线视频| 久久久www成人免费毛片| 国产高清免费的视频| 亚洲视频在线免费| 久久久久久亚洲精品| 香蕉大伊亚洲人在线观看| 国产亚洲福利一区二区免费看 | 免费在线不卡视频| 亚洲成AV人片一区二区密柚| 亚洲成人高清在线观看| 特级一级毛片免费看| 未满十八18禁止免费无码网站| 最近中文字幕mv免费高清电影|