1 importjava.sql.Connection;2 importjava.sql.DriverManager;3 importjava.sql.PreparedStatement;4 importjava.sql.ResultSet;5 importjava.sql.SQLException;6 importjava.sql.Statement;7
8
9
10 public classJDBCDemo01 {11 public static voidmain(String[] args) {12 selectAll();13 //登录校验
14 selectByUsernamePassword("Micheal", "123");15 selectByUP2("Micheal", "123");16 //分页查询
17 selectUserByPage(3, 4);18 insert("ky", "123456");19 delete(3);20 update(2, "123456");21
22 }23
24 public static voidselectAll() {25
26 Connection con = null;27 Statement stmt = null;28 ResultSet rs = null;29
30 try{31 con =JDBCUtils.getConnection();32
33 stmt =con.createStatement();34 rs = stmt.executeQuery("select * from user");35
36 while(rs.next()) {37 System.out.println(rs.getInt("id") + "," + rs.getString(rs.getString("username")) + "," + rs.getString("password"));38
39 }40
41 } catch(Exception e) {42 //TODO Auto-generated catch block
43 e.printStackTrace();44 } finally{45 JDBCUtils.close(rs, stmt, con);46 }47 }48
49 public static booleanselectByUsernamePassword(String username, String password) {50 Statement stmt = null;51 Connection con = null;52 ResultSet rs = null;53 try{54 Class.forName("com.mysql.cj.jdbc.Driver");55
56 String url = "jdbc:mysql://localhost:3306/web01?useUnicode = true & characterEncoding