package com.aaa.demo1; import com.aaa.utils.JdbcUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class DemoLogin { 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 pwd = sc.nextLine(); login(name,pwd); } //登陆方法 public static void login(String name,String password) throws SQLException { Connection conn = JdbcUtils.getConnection(); //1编辑SQL语句 String sql="select * from users where uname=? and uage=?"; PreparedStatement pre = conn.prepareStatement(sql); pre.setString(1,name); pre.setString(2,password); ResultSet rs = pre.executeQuery(); if(rs.next()){ System.out.println("登陆成功!"); }else{ System.out.println("登陆失败"); } //关闭资源 JdbcUtils.close(rs,pre,conn); } }