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 }