public static void main(String[] args) throws SQLException {
//从控制台输入用户名和密码
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户名");
String name= sc.nextLine();
System.out.println("请输入密码");
String password=sc.nextLine();
//登陆方法
login(name,password);
}
private static void login(String name,String password) throws SQLException {
Connection conn = JdbcUtils.getConnection();
//编写sql语句 未知内容使用? 占位符
String s = "select * from ma where aname=? and money=?";
//获取PreparedStatement对象
PreparedStatement pr = conn.prepareStatement(s);
//设置实际的参数 setxx(占位符的位置,真实的值);
pr.setString(1,name);
pr.setString(2,password);
//执行sql语句
ResultSet re =pr.executeQuery();
if (re.next()){
System.out.println("登陆成功");
}else {
System.out.println("登陆失败");
}
//关闭流
JdbcUtils.close(conn,pr,re);
}
转载于:https://www.cnblogs.com/wwwbobocom/p/10646527.html