1.4 JDBC "连接"Mysql 数据库 >我的程序猿之路: 第四章

 1 public class hh {
 2     public static void main(String[] args) {
 3         try {
 4             hh.getsqlConn();
 5         } catch (Exception e) {
 6             // TODO Auto-generated catch block
 7             e.printStackTrace();
 8         }
 9     }
10 
11     public static void getsqlConn() throws Exception {
12         // 加载驱动
13         DriverManager.registerDriver(new com.mysql.jdbc.Driver());
14         // 建立连接
15         Connection conn = DriverManager.getConnection(
16                 "jdbc:mysql://localhost:3306/fan", "root", "123");
17         //创建语句
18         Statement sm= conn.createStatement();
19         //执行语句
20         ResultSet rs = sm.executeQuery("Select * from user");
21         while(rs.next()){
22             System.out.println(rs.getObject(2));
23         }
24         rs.close();
25         sm.close();
26         conn.close();
27         
28     }
29 
30 }

 

posted @ 2017-05-05 17:59  奕语∮梵天  阅读(121)  评论(0编辑  收藏  举报