获取用户输入的用户和密码,然后使用封装后的jdbc来查库进行验证用户名和密码是否正确

复制代码
public class Test02 {
//    获取用户输入的用户和密码,然后使用封装后的jdbc来查库进行验证用户名和密码是否正确
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection coon = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/db4?useSSL=false&useUnicode=true&characterEncoding=UTF-8",
                "root",
                "123456");
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入用用户名:");
        String name = scanner.next();
        System.out.print("请输入密码:");
        int pwd = scanner.nextInt();
        String sql = "select * from admin where name ='" + name +"' and pwd = " + pwd ;
        Statement statement = coon.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        if (resultSet.next()){
            System.out.println("密码输入正确");
        }else {
            System.out.println("输入错误");
        }
        resultSet.close();
        statement.close();
        coon.close();
    }
}
复制代码

 

封装jdbc然后 改造

复制代码
public class JdbcUtils {
    private static String url;
    private static String user;
    private static String pwd;
    private static String driver;
    static {
        ClassLoader classLoader = JdbcUtils.class.getClassLoader();
        Properties properties = new Properties();
        try {
            properties.load(classLoader.getResourceAsStream("jdbc.properties"));
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            pwd = properties.getProperty("pwd");
            driver = properties.getProperty("driver");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //获得连接对象
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,pwd);
    }
    //关闭连接
    public static void close(ResultSet resultSet, Statement statement,Connection conn){
        try {
            if (resultSet != null){
                resultSet.close();
            }
            if (statement != null){
                statement.close();
            }
            if (conn != null){
                conn.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
    public static void close(Statement statement,Connection conn){
        try {
            if (statement != null){
                statement.close();
            }
            if (conn != null){
               conn.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
复制代码
url=jdbc:mysql://localhost:3306/db4?useSSL=false&useUnicode=true&characterEncoding=utf-8
user=root
pwd=123456
driver=com.mysql.jdbc.Driver
复制代码
public class Test02 {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入用户名:");
        String name = scanner.next();
        System.out.print("请输入密码:");
        int pwd = scanner.nextInt();
        Connection conn = JdbcUtils.getConnection();
        Statement statement = conn.createStatement();
        String sql = "select * from admin where name ='" + name +"' and pwd = " + pwd ;
        ResultSet resultSet = statement.executeQuery(sql);
        if (resultSet.next()){
            System.out.println("密码正确");
        }else {
            System.out.println("密码错误");
        }
        JdbcUtils.close(statement,conn);
    }
}
复制代码

 

posted @   阿文程序猿  阅读(63)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示