Welcom to RO_wsy's blog

巨简单的jdbc程序

刚学jdbc,来个最简单的吧,代码如下:

 1 import java.sql.*;
 2 import javax.sql.*;
 3 
 4 public class TestJDBC {
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) throws Exception{
 9         // TODO Auto-generated method stub
10         test();
11         
12     }
13     
14     public static void test() throws Exception {
15         // 1.register
16         DriverManager.registerDriver(new com.mysql.jdbc.Driver());
17         
18         // 2.create connection
19         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "123456");
20         
21         // 3.create statement
22         Statement st = conn.createStatement();
23         
24         // 4.execute statement
25         ResultSet rs = st.executeQuery("select * from user");
26         
27         // 5.deal with result
28         while (rs.next()) {
29             System.out.println(rs.getObject(1) + ", " + rs.getObject(2));
30         }
31         
32         // 6.release resource
33         rs.close();
34         st.close();
35         conn.close();
36     }
37 }

搞定。。。

posted @ 2012-11-23 22:31  RO_wsy  阅读(174)  评论(0编辑  收藏  举报