登录验证
import java.sql.*;
import java.util.Scanner;
public class Loginjdbc2 {
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String username = sc.nextLine();
System.out.println("请输入密码");
String password = sc.nextLine();
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/learning?useSSL=false", "root", "li1579026891");
//获得PreparedStatement对象 预编译SQL语句
PreparedStatement preparedStatement = connection.prepareStatement("select * from user where username=? and password=?");
//为?占位符 赋值
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
//执行SQL语句,并接收结果
ResultSet resultSet = preparedStatement.executeQuery();
//处理结果
if (resultSet.next()){
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
//关闭资源
connection.close();
preparedStatement.close();
resultSet.close();
}
}