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 阅读(909) 评论(0)  编辑  收藏 所属分类: JAVA

只有注册用户登录后才能发表评论。


网站导航:
 

常用链接

留言簿(21)

随笔分类(265)

随笔档案(242)

相册

JAVA网站

关注的Blog

搜索

  •  

积分与排名

  • 积分 - 909069
  • 排名 - 40

最新评论