模拟登陆验证:
1 package JdbcDome; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.util.Scanner; 7 8 public class JDbcDome2 { 9 public static void main(String[] args) throws Exception { 10 Scanner sc = new Scanner(System.in); 11 System.out.println("请您输入账户名"); 12 String name = sc.nextLine(); 13 System.out.println("请您输入密码"); 14 String pd = sc.nextLine(); 15 login(name,pd); 16 } 17 public static void login(String name,String pd) throws Exception { 18 Connection conn = JDBCUtils.getConnection(); 19 //1 编写SQl语句,未知内容使用? (?为占位符) 20 String sql = "select * from zh where uname = ? and upd = ?"; 21 //2 获得PreparedStatement对象。 22 PreparedStatement ps = conn.prepareStatement(sql); 23 //3 设置实际的参数 setxxx(占位符的位置,真实的值。) 24 ps.setString(1,name); 25 ps.setString(2,pd); 26 //4 执行SQL语句。 27 ResultSet rs = ps.executeQuery(); 28 if (rs.next()){ 29 System.out.println("登陆成功"); 30 }else{ 31 System.out.println("登录失败"); 32 } 33 //5 关闭资源。 34 JDBCUtils.close(conn,ps, rs); 35 } 36 }
原文:https://www.cnblogs.com/lxr521/p/10645188.html