package org.example.test; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class JdbcLoginTwo { public static void main(String[] args) throws SQLException, ClassNotFoundException { //输入部分 Scanner scanner=new Scanner(System.in); System.out.println("用户名:"); String username= scanner.next(); System.out.println("密码:"); String password=scanner.next(); //判断用户名和密码是否在数据库中存在,存在返回登录成功,不存在返回登录失败 //? 占位符 String find_username_password_sql="select * from user where username='"+username+"' and password='"+password+"'"; //获取连接 Connection connection= JDBCUtils.getConnection(); //创建PreparedStatement对象,并载入SQL语句 PreparedStatement statement=connection.prepareStatement(find_username_password_sql); statement.setString(1,username); statement.setString(2,password); //执行SQL ResultSet resultSet=statement.executeQuery(); //判断有没有结果集 if (resultSet.next()){ System.out.println("登录成功!"); } else { System.out.println("登录失败!"); } //释放资源 JDBCUtils.close(resultSet,statement,connection); } }