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 }
原文地址:https://www.cnblogs.com/fanyuyi-boke/p/qiao_duo_shao_nian_dai_ma_neng_ba_shou_zhi_mo_ping4.html