java+mysql构建一个登录注册系统

1、连接数据库类


package 登录注册系统;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {
    private static final String DRIVER = "com.mysql.jdbc.Driver"; // 加载驱动
    private static final String URL = "jdbc:mysql://localhost:3306/zoo?useUnicode=true&chacaterEncoding=utf-8&useSSL=false"; // 连接数据库
    private static final String USER = "root"; // 数据库用户名
    private static final String PASSWORD = "123456"; // 数据库密码

    public static Connection getConnection() { // 封装开启连接类
        Connection con = null;
        try {
            Class.forName(DRIVER);
            con = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            // TODO: handle exception
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return con;
    }

    public static void close(Connection con) { // 以下都是封装关闭连接类
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }

    public static void close(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }

    public static void close(PreparedStatement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }

    public static void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }

}


 


2、封装各种查询,插入,更新的方法

package 登录注册系统;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JOptionPane;;

public class TestMysql {
    private static final Connection CONNECTION = Test.getConnection();

    public static void InsertData(String name, String account, int password, String email, String id_card,
            String telephone, String address) { // 注册信息
        PreparedStatement STATEMENT;
        String sql = "insert into pm values(?,?,?,?,?,?,?);"; // SQL插入语句模板,总共需要插入七个数据
        try {
            STATEMENT = (PreparedStatement) CONNECTION.prepareStatement(sql);

            STATEMENT.setString(1, name);
            STATEMENT.setString(2, account);
            STATEMENT.setInt(3, password);
            STATEMENT.setString(4, email);
            STATEMENT.setString(5, id_card);
            STATEMENT.setString(6, telephone);
            STATEMENT.setString(7, address);

            STATEMENT.executeUpdate();

        } catch (SQLException e) {
            JOptionPane.showConfirmDialog(null, "注册失败!");
        }
    }

    public static boolean FindData(String account) { // 判断是否账号重复
        Statement STATEMENT = null;
        String sql = "select count(account) from pm where account='" + account + "'";
        if (account.length() != 0) {
            try {
                STATEMENT = CONNECTION.createStatement();
                ResultSet rs = STATEMENT.executeQuery(sql);
                int count = 0;
                while (rs.next()) {
                    count = rs.getInt(1); // 获取sql语句返回值
                }

                if (count != 0) { // 若不为0,则说明账号已经存在
                    return false;
                } else {
                    return true;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static int Validation(String account) { // 查询密码
        Statement STATEMENT = null;
        String sql = "select password from pm where account='" + account + "'";
        int password = 0;
        try {
            STATEMENT = CONNECTION.createStatement();
            ResultSet rs = STATEMENT.executeQuery(sql);
            while (rs.next()) {
                password = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return password;
    }

    public static boolean Login(String account, String password) {
        Statement STATEMENT = null;
        String sql = "select password from pm where account='" + account + "'";
        if (TestMysql.FindData(account)) {
            JOptionPane.showConfirmDialog(null, "账号不存在!");
        } else {
            try {
                String passString = null;
                STATEMENT = CONNECTION.createStatement();
                ResultSet rs = STATEMENT.executeQuery(sql);
                while (rs.next()) {
                    passString = rs.getString(1);
                }
                if (passString.equals(password)) {
                    return true;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static boolean UpdatePassword(int origin_password, int new_password) { // 更改密码
        Statement STATEMENT = null;
        String sql = "update pm set password = replace(password," + origin_password + "," + new_password + ")";
        if (origin_password == new_password) {
            return false;
        } else {
            try {
                STATEMENT = CONNECTION.createStatement();
                STATEMENT.executeUpdate(sql);
            } catch (SQLException e) {
                return false;
            }
        }

        return true;
    }

    public static boolean Forget_password(String account, String email, String id_card) {
        Statement STATEMENT = null;

        String sql = "select account,email,id_card from pm where account = '" + account + "'";

        try {
            String accountString = null;
            String emailString = null;
            String id_cardString = null;
            STATEMENT = CONNECTION.createStatement();
            ResultSet rs = STATEMENT.executeQuery(sql);
            while (rs.next()) {
                accountString = rs.getString(1);
                emailString = rs.getString(2);
                id_cardString = rs.getString(3);
            }
            System.out.println(account);
            System.out.println(email);
            System.out.println(id_card);

            System.out.println(accountString);
            System.out.println(emailString);
            System.out.println(id_cardString);
            if (account.equals(accountString) && email.equals(emailString) && id_card.equals(id_cardString)) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean Validation2(int password, String account) {
        Statement STATEMENT = null;
        String sql = "update pm set password=" + password + " where account= '" + account + "'";
        try {
            STATEMENT = CONNECTION.createStatement();
            STATEMENT.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }
}


 


3、验证注册信息的类


package 登录注册系统;

import java.util.regex.Pattern;

public class MessageMatch {
    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; // 验证邮箱
    public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,20}$"; // 验证用户名
    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$"; // 验证密码
    public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"; // 验证手机号
    public static final String REGEX_ID_CARD = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|"
            + "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";// 验证身份证
    public static final String REGEX_CHINESE = "[\u4e00-\u9fa5]+"; // 验证地址

    public static boolean isPassword(String password) { // 验证密码
        return Pattern.matches(REGEX_PASSWORD, password);
    }

    public static boolean isMobile(String mobile) { // 验证手机号
        return Pattern.matches(REGEX_MOBILE, mobile);
    }

    public static boolean isEmail(String email) { // 验证邮箱
        return Pattern.matches(REGEX_EMAIL, email);
    }

    public static boolean isUsername(String username) { // 验证用户名
        return Pattern.matches(REGEX_CHINESE, username);
    }

    public static boolean isIDCard(String idCard) { // 验证身份证
        return Pattern.matches(REGEX_ID_CARD, idCard);
    }

    public static boolean isAddress(String address) { // 验证姓名
        return Pattern.matches(REGEX_CHINESE, address);
    }

}


 


4、主类,登录界面


package 登录注册系统;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JFrame {
    JLabel label1, label2;
    JTextField account;
    JPasswordField password;
    JButton login, register, modify_password, Forget_password;
    JPanel p;

    private static final Connection CONNECTION = Test.getConnection();
    private static Statement STATEMENT = null;

    public Login() {
        setLayout(new BorderLayout());

        label1 = new JLabel("账号:");
        label2 = new JLabel("密码:");
        account = new JTextField(25);
        password = new JPasswordField(25);
        login = new JButton("登录");
        register = new JButton("注册");
        modify_password = new JButton("修改密码");
        Forget_password = new JButton("忘记密码");
        p = new JPanel();

        p.add(label1);
        p.add(account);
        p.add(label2);
        p.add(password);
        p.add(login);
        p.add(register);
        p.add(modify_password);
        p.add(Forget_password);

        add(p, BorderLayout.CENTER);

        login.addActionListener(event -> {
            if (account.getText().length() != 0 && String.valueOf(password.getPassword()).length() != 0) {
                if (TestMysql.Login(account.getText(), String.valueOf(password.getPassword()))) {
                    JOptionPane.showConfirmDialog(null, "登陆成功!");
                } else {
                    JOptionPane.showConfirmDialog(null, "登陆失败!");
                }
            } else {
                JOptionPane.showConfirmDialog(null, "输入不能为空!");
            }
        });

        register.addActionListener(event -> {
            EventQueue.invokeLater(() -> {
                JFrame frame = new Register();
                frame.setTitle("注册界面");
                frame.setBounds(700, 300, 300, 700);
                frame.setVisible(true);
                frame.setResizable(false);
            });
        });
        modify_password.addActionListener(event -> {
            EventQueue.invokeLater(() -> {
                JFrame frame = new Modify_password();
                frame.setTitle("修改密码界面");
                frame.setBounds(700, 300, 300, 400);
                frame.setVisible(true);
                frame.setResizable(false);
            });
        });

        Forget_password.addActionListener(event -> {
            EventQueue.invokeLater(() -> {
                JFrame Frame = new Forget_password();
                Frame.setBounds(700, 300, 300, 200);
                Frame.setTitle("忘记密码");
                Frame.setVisible(true);
                Frame.setResizable(false);
            });
        });
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            JFrame loginFrame = new Login();
            loginFrame.setBounds(700, 300, 350, 150);
            loginFrame.setTitle("登录界面");
            loginFrame.setVisible(true);
            loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            loginFrame.setResizable(false);
        });
    }
}


 


5、注册界面


package 登录注册系统;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JFrame {
    JLabel label1, label2;
    JTextField account;
    JPasswordField password;
    JButton login, register, modify_password, Forget_password;
    JPanel p;

    private static final Connection CONNECTION = Test.getConnection();
    private static Statement STATEMENT = null;

    public Login() {
        setLayout(new BorderLayout());

        label1 = new JLabel("账号:");
        label2 = new JLabel("密码:");
        account = new JTextField(25);
        password = new JPasswordField(25);
        login = new JButton("登录");
        register = new JButton("注册");
        modify_password = new JButton("修改密码");
        Forget_password = new JButton("忘记密码");
        p = new JPanel();

        p.add(label1);
        p.add(account);
        p.add(label2);
        p.add(password);
        p.add(login);
        p.add(register);
        p.add(modify_password);
        p.add(Forget_password);

        add(p, BorderLayout.CENTER);

        login.addActionListener(event -> {
            if (account.getText().length() != 0 && String.valueOf(password.getPassword()).length() != 0) {
                if (TestMysql.Login(account.getText(), String.valueOf(password.getPassword()))) {
                    JOptionPane.showConfirmDialog(null, "登陆成功!");
                } else {
                    JOptionPane.showConfirmDialog(null, "登陆失败!");
                }
            } else {
                JOptionPane.showConfirmDialog(null, "输入不能为空!");
            }
        });

        register.addActionListener(event -> {
            EventQueue.invokeLater(() -> {
                JFrame frame = new Register();
                frame.setTitle("注册界面");
                frame.setBounds(700, 300, 300, 700);
                frame.setVisible(true);
                frame.setResizable(false);
            });
        });
        modify_password.addActionListener(event -> {
            EventQueue.invokeLater(() -> {
                JFrame frame = new Modify_password();
                frame.setTitle("修改密码界面");
                frame.setBounds(700, 300, 300, 400);
                frame.setVisible(true);
                frame.setResizable(false);
            });
        });

        Forget_password.addActionListener(event -> {
            EventQueue.invokeLater(() -> {
                JFrame Frame = new Forget_password();
                Frame.setBounds(700, 300, 300, 200);
                Frame.setTitle("忘记密码");
                Frame.setVisible(true);
                Frame.setResizable(false);
            });
        });
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            JFrame loginFrame = new Login();
            loginFrame.setBounds(700, 300, 350, 150);
            loginFrame.setTitle("登录界面");
            loginFrame.setVisible(true);
            loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            loginFrame.setResizable(false);
        });
    }
}


 


6、修改密码界面


package 登录注册系统;

import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Modify_password extends JFrame {
    JLabel accountJLabel, originlJLabel, newJLabel; // 要修改的账号,原密码,新密码标签
    JTextField account; // 要修改的账号,原密码,新密码文本框
    JPasswordField origin_password, new_password;
    JButton confirm, cancel; // 确认和取消
    JPanel p; // 面板

    private static final Connection CONNECTION = Test.getConnection();
    private static Statement STATEMENT = null;

    public Modify_password() {
        setLayout(new BorderLayout());
        accountJLabel = new JLabel("本账号:");
        originlJLabel = new JLabel("原密码:");
        newJLabel = new JLabel("新密码:");

        account = new JTextField(20);
        origin_password = new JPasswordField(20);
        new_password = new JPasswordField(20);

        confirm = new JButton("确认");
        cancel = new JButton("取消");
        p = new JPanel();

        p.add(accountJLabel);
        p.add(account);
        p.add(originlJLabel);
        p.add(origin_password);
        p.add(newJLabel);
        p.add(new_password);
        p.add(confirm);
        p.add(cancel);
        add(p, BorderLayout.CENTER);

        confirm.addActionListener(event -> {
            int oripwd = TestMysql.Validation(account.getText());
            if (Arrays.equals(String.valueOf(oripwd).toCharArray(), origin_password.getPassword())) {
                String newpassword = String.valueOf(new_password.getPassword());

                if (account.getText().length() != 0 && String.valueOf(origin_password.getPassword()).length() != 0
                        && String.valueOf(new_password).length() != 0) {
                    if (TestMysql.UpdatePassword(oripwd, Integer.parseInt(newpassword))) {
                        JOptionPane.showConfirmDialog(null, "修改成功!");
                        account.setText("");
                        origin_password.setText("");
                        new_password.setText("");
                    } else {
                        JOptionPane.showConfirmDialog(null, "修改失败!");
                        account.setText("");
                        origin_password.setText("");
                        new_password.setText("");
                    }
                } else {
                    JOptionPane.showConfirmDialog(null, "输入不能为空!");
                }

            } else {
                JOptionPane.showConfirmDialog(null, "原密码错误!");
            }

        });

        cancel.addActionListener(event -> {
            this.dispose();
        });
    }
}


 


7、忘记密码界面


package 登录注册系统;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Forget_password extends JFrame {
    JLabel emaiLabel, id_cardLabel, passwordLabel, accountLabel;
    JTextField emailField, id_cardField, accountField;
    JPasswordField passwordField;
    JButton confirmButton, cancelButton;
    JPanel p;

    public Forget_password() {
        setLayout(new BorderLayout());
        emaiLabel = new JLabel("邮箱:");
        id_cardLabel = new JLabel("身份:");
        passwordLabel = new JLabel("密码:");
        accountLabel = new JLabel("账号:");
        emailField = new JTextField(20);
        id_cardField = new JTextField(20);
        passwordField = new JPasswordField(20);
        accountField = new JTextField(20);
        confirmButton = new JButton("确认");
        cancelButton = new JButton("取消");
        p = new JPanel();

        p.add(accountLabel);
        p.add(accountField);

        p.add(emaiLabel);
        p.add(emailField);

        p.add(id_cardLabel);
        p.add(id_cardField);

        p.add(passwordLabel);
        p.add(passwordField);

        p.add(confirmButton);
        p.add(cancelButton);
        add(p, BorderLayout.CENTER);

        confirmButton.addActionListener(event -> {
            if (emailField.getText().length() != 0 && id_cardField.getText().length() != 0
                    && String.valueOf(passwordField.getPassword()).length() != 0) {
                if (TestMysql.Forget_password(accountField.getText(), emailField.getText(), id_cardField.getText())) {
                    if (TestMysql.Validation2(Integer.parseInt(String.valueOf(passwordField.getPassword())),
                            accountField.getText())) {
                        JOptionPane.showConfirmDialog(null, "修改密码成功!");
                    } else {
                        JOptionPane.showConfirmDialog(null, "修改密码失败!");
                    }

                } else {
                    JOptionPane.showConfirmDialog(null, "输入信息有误!请重新确认!");
                }
            } else {
                JOptionPane.showConfirmDialog(null, "信息不能为空!");
            }
        });

        cancelButton.addActionListener(event -> {
            this.dispose();
        });
    }

}


 


刚入门的小白一个,有错误请大家多多指正!

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值