public void login1(String username, String password) throws ClassNotFoundException, SQLException { // 1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web08", "root", "root"); // 3.编写sql语句 String sql = "select * from tbl_user where uname=? and upassword=?"; // 4.创建预处理对象 PreparedStatement pstmt = conn.prepareStatement(sql); // 5.设置参数(给占位符) pstmt.setString(1, username); pstmt.setString(2, password); // 6.执行查询操作 ResultSet rs = pstmt.executeQuery(); // 7.对结果集进行处理 if (rs.next()) { System.out.println("恭喜您," + username + ",登录成功!"); System.out.println(sql); } else { System.out.println("账号或密码错误!"); } if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } }
年与时驰,意与日去,遂成枯落,
多不接世,悲守穷庐,将复何及。