• JDBC-登陆功能实现


    1.user.java

    package songyan.jdbc.entity;
    
    import java.util.Date;
    
    public class User {
        private int id;
        private String name;
        private String password;
        private String email;
        private Date birthday;
        
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        public Date getBirthday() {
            return birthday;
        }
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
        
        
        
        
    }

    2.idbinfo.properties

    driverClass=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/day06
    username=root
    password=247418

    3.Util.java

    package songyan.jdbc.util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ResourceBundle;
    
    public class Util {
        private static String driverClass;
        private static String url;
        private static String username;
        private static String password;
        
        static{
            ResourceBundle rb =ResourceBundle.getBundle("dbinfo");
            driverClass=rb.getString("driverClass");
            url=rb.getString("url");
            username=rb.getString("username");
            password=rb.getString("password");
            try {
                Class.forName(driverClass);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        
        public static Connection getConnection() throws SQLException
        {
            return DriverManager.getConnection(url,username,password);
        }
        
        public static void closeAll(Connection conn,Statement sta,ResultSet rs)
        {
            if(rs!=null)
            {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                rs=null;
            }
            if(sta!=null)
            {
                try {
                    sta.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                sta=null;
            }
            if(conn!=null)
            {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                conn=null;
            }
        }
        
    
    }

    4.DoLogin.java

    package songyan.jdbc.service;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    import songyan.jdbc.entity.User;
    import songyan.jdbc.login.*;
    import songyan.jdbc.util.Util;
    
    public class DoLogin {
        public static User findUser(String name,String password) 
        {
            Connection conn=null;
            Statement sta=null;
            ResultSet rs=null;
            User u=null;
            
            try {
            conn=Util.getConnection();
            sta=conn.createStatement();
            rs=sta.executeQuery(
                    "select * from users where name='"+name
                    +"' and password='"+password+"'");
            if(rs.next())
            {
                u= new User();
                u.setId(rs.getInt("id"));
                u.setName(rs.getString("name"));
                u.setPassword(rs.getString("password"));
                u.setEmail(rs.getString("email"));
                u.setBirthday(rs.getDate("birthday"));    
                
            }
            
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally{
                Util.closeAll(conn, sta, rs);
                
            }
            return u;
        }
        
        public static void main(String[] args)
        {
            
        }
    }

    5.Login.java

    package songyan.jdbc.login.client;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    import songyan.jdbc.login.*;
    import songyan.jdbc.service.DoLogin;
    import songyan.jdbc.entity.User;
    
    public class Login {
        public static void main(String[] args) throws SQLException
        {
            
            System.out.println("请输入用户名:");
            Scanner in = new Scanner(System.in);
            String name=in.next();
            System.out.println("请输入密码:");
            String password=in.next();
            
            User u=DoLogin.findUser(name, password);
            if(u==null)
            {
                System.out.println("密码不正确");
            }
            else{
                System.out.println("登陆成功");
            }
            
        
        }
    }
  • 相关阅读:
    iOS 文件操作--归档和解档
    iOS中UITabBarController的使用
    Objective-C基础知识点总结,字符串操作,数组操作,字典操作
    Objective-C中协议和分类总结
    Objective-C文件操作之NSCoding协议之小练习
    浅谈Objective-C继承和多态
    Objective-C内存管理基础知识
    MySort(选做)的实现
    20175308 2018-2019-2 实验四 《Android开发基础》实验报告
    JAVA 第十一周学习总结
  • 原文地址:https://www.cnblogs.com/excellencesy/p/8544820.html
Copyright © 2020-2023  润新知