代码实现
package com.fy.jdbc;
import java.sql.*;
import java.util.Scanner;
public class TestLogin {
public static void main(String[] args) throws Exception {
System.out.println("请输入用户名");
String user = new Scanner(System.in).nextLine();
System.out.println("请输入密码");
String pwd = new Scanner(System.in).nextLine();
login("root","1234");
}
public static void login(String user,String pwd) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day0901", "root", "1234");
String sql = "select * from tb3 where user = ? and pwd = ?";
PreparedStatement p = conn.prepareStatement(sql);
p.setString(1, user);
p.setString(2, pwd);
ResultSet set = p.executeQuery();
System.out.println(sql);
if(set.next()) {
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
}
}
代码运行结果
![代码运行结果](https://i-blog.csdnimg.cn/blog_migrate/3a6169eb7db14e78d97edb3e71991dd1.png#pic_center)
数据库数据
![数据库表数据](https://i-blog.csdnimg.cn/blog_migrate/1b40b1f763e207ced77d0e0d34b89a8a.png#pic_center)