<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 測試數(shù)據(jù)庫連接,以及從數(shù)據(jù)庫中簡單讀取數(shù)據(jù)
      6 //
      7 //  @author hcm
      8 //
      9 //  @date 2006-12
     10 //
     11 //  [注]:首先建立access數(shù)據(jù)庫 test ,然后建立數(shù)據(jù)表 test ,寫入一些測試數(shù)據(jù),其次建立數(shù)據(jù)源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 ("初始化驅(qū)動程序異常:"+e.getMessage ());
     38         }
     39         try
     40         {
     41             con = DriverManager.getConnection ("jdbc:odbc:test");
     42             /*參數(shù)一:
     43              ResultSet.TYPE_FORWARD_ONLY
     44              ResultSet.TYPE_SCROLL_INSENSITIVE 
     45              或 ResultSet.TYPE_SCROLL_SENSITIVE 
     46              */
     47             /*參數(shù)二:
     48              *   ResultSet.CONCUR_READ_ONLY 
     49              或 ResultSet.CONCUR_UPDATABLE 之一 
     50              */
     51             //生成可更新可滾動 狀態(tài)集
     52             stm = con.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
     53         }catch(SQLException e)
     54         {
     55             System.out.println ("連接初始化發(fā)生異常:"+e.toString ());
     56         }
     57         
     58     }
     59     
     60     //插入數(shù)據(jù)
     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("插入數(shù)據(jù)時發(fā)生異常:" + sqlexception.getMessage());
     70         }
     71     }
     72     //刪除數(shù)據(jù)
     73     public void delData(String sql)
     74     {
     75          try
     76         {
     77           stm.executeUpdate(sql);
     78         }
     79         catch(SQLException e)
     80         {
     81             System.err.println("刪除數(shù)據(jù)失敗: " + e.getMessage());
     82         }
     83     }
     84     //查詢數(shù)據(jù)
     85     public ResultSet  queryData ( String sql)
     86     {
     87       try{
     88            rs = stm.executeQuery (sql);               
     89         }
     90         catch(SQLException e)
     91         {
     92             System.out.println ("數(shù)據(jù)庫連接異常:"+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     //執(zhí)行更新
    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("更新數(shù)據(jù)庫發(fā)生異常: " + sqlexception.getMessage());
    113         }
    114         return i;
    115     }
    116     //關(guān)閉連接
    117     public void close()
    118     {
    119         try
    120         {
    121             stm.close();
    122             con.close();
    123         }
    124         catch(SQLException sqlexception)
    125         {
    126             System.err.println("關(guān)閉數(shù)據(jù)庫異常: " + 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 ("遍歷查詢結(jié)果時異常:"+e.toString ());
    150         }
    151         catch(Exception e)
    152         {
    153             System.out.println ("發(fā)生:"+e.toString ()+" 異常");
    154         }
    155         
    156         /**********************************************************************************/
    157         /**********************************************************************************/
    158         
    159     //測試刪除數(shù)據(jù)
    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("刪除數(shù)據(jù)發(fā)生:"+e.getMessage ());
    168         }
    169         /**********************************************************************************/
    170         /**********************************************************************************/
    171     //測試插入數(shù)據(jù)
    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("插入數(shù)據(jù):"+e.getMessage());
    179         }
    180         /**********************************************************************************/
    181         /**********************************************************************************/
    182         //測試更新數(shù)據(jù)
    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("更新數(shù)據(jù)發(fā)生:"+e.getMessage());
    190         }
    191         /**********************************************************************************/
    192         //關(guān)閉連接
    193         exec.close();
    194     }
    195     
    196 }
    197 

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

    常用鏈接

    留言簿(21)

    隨筆分類(265)

    隨筆檔案(242)

    相冊

    JAVA網(wǎng)站

    關(guān)注的Blog

    搜索

    •  

    積分與排名

    • 積分 - 914554
    • 排名 - 40

    最新評論

    主站蜘蛛池模板: 色多多A级毛片免费看| 亚洲日韩精品无码专区加勒比☆| 亚洲综合久久综合激情久久| 亚洲综合婷婷久久| 亚洲人成77777在线观看网| 日本亚洲高清乱码中文在线观看| 国产男女爽爽爽免费视频| 免费一级毛片无毒不卡| 西西大胆无码视频免费| 免费大香伊蕉在人线国产 | 女人让男人免费桶爽30分钟| 国产午夜鲁丝片AV无码免费| 中文字幕亚洲乱码熟女一区二区 | 亚洲av永久无码天堂网| 午夜不卡AV免费| 99视频有精品视频免费观看| 好大好深好猛好爽视频免费| 国产亚洲精品影视在线产品 | 亚洲高清视频免费| 亚洲av无码一区二区三区人妖 | 在线免费观看你懂的| 国产精品嫩草影院免费| 亚洲一区精品无码| 亚洲影视自拍揄拍愉拍| 国产区在线免费观看| 国内免费高清在线观看| 亚洲人成影院在线无码按摩店 | 亚洲一区精品中文字幕| 日韩成人精品日本亚洲| 国产成人精品免费久久久久| 日本黄色免费观看| 亚洲国产一区二区三区青草影视| 亚洲AV日韩AV一区二区三曲| 日韩电影免费在线观看| 四虎永久免费影院在线| 91亚洲一区二区在线观看不卡| 国产精品亚洲五月天高清| 蜜桃AV无码免费看永久| 精品国产亚洲一区二区在线观看| 亚洲国产美女在线观看| 两性色午夜免费视频|