数据库简单操作—注册和登录实例

  本例是创建用户注册和登录的数据库操作,通过Swing组件实现注册和登录界面的实现。

注册

功能实现

注册实现:
这里写图片描述

判断用户名输入是否合法:
这里写图片描述

判断密码输入是否合法:
这里写图片描述

判断两次密码输入是否一致:
这里写图片描述

注册代码

//SQLManager类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

//采用单例设计模式
public class SQLManager {
    private Statement statement;
    //获得statement
    public Statement getStatement() {
        return statement;
    }
    //返回statement
    public void setStatement(Statement statement) {
        this.statement = statement;
    }
    private static SQLManager manager;
    private SQLManager() {
        //连接数据库的驱动
                String driver = "com.mysql.jdbc.Driver";
                //指向数据库访问地址
                String url = "jdbc:mysql://localhost:3306/clazz";
                //数据库的用户名
                String user = "root";
                //数据库用户密码
                String password = "123456";

                try {
                    //加载驱动
                    Class.forName(driver);
                    //与数据库建立连接
                    Connection conn = DriverManager.getConnection(url,user, password);
                    if(!conn.isClosed()){
                        //创建数据库操作类
                        statement = conn.createStatement();
                    }else{
                        System.out.println("请打开数据库!");
                    }

                } catch (ClassNotFoundException e) {            
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
    }
    /**
     * 创建该类的对象
     * @return 返回SQLManager对象。
     */
    public static synchronized SQLManager newInstance(){
        if(manager==null){
            manager = new SQLManager();
        }
        return manager;
    }
}
//登录界面
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.event.ActionEvent;

public class MySQL extends JFrame {

    private JPanel contentPane;
    private JTextField textFieldUserName;
    private JTextField textFieldPassword;
    private JTextField textFieldPasswordAgain;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MySQL frame = new MySQL();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MySQL() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 453, 395);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textFieldUserName = new JTextField();
        textFieldUserName.setBounds(125, 77, 133, 21);
        contentPane.add(textFieldUserName);
        textFieldUserName.setColumns(10);

        JLabel lblNewLabelUser = new JLabel("用户名:");
        lblNewLabelUser.setBounds(40, 80, 54, 15);
        contentPane.add(lblNewLabelUser);

        textFieldPassword = new JTextField();
        textFieldPassword.setBounds(125, 120, 133, 21);
        contentPane.add(textFieldPassword);
        textFieldPassword.setColumns(10);

        JLabel lblNewLabelPassword = new JLabel("密码:");
        lblNewLabelPassword.setBounds(40, 123, 54, 15);
        contentPane.add(lblNewLabelPassword);

        textFieldPasswordAgain = new JTextField();
        textFieldPasswordAgain.setBounds(125, 175, 133, 21);
        contentPane.add(textFieldPasswordAgain);
        textFieldPasswordAgain.setColumns(10);

        JLabel lblNewLabelPasswordAgain = new JLabel("确认密码:");
        lblNewLabelPasswordAgain.setBounds(40, 178, 75, 15);
        contentPane.add(lblNewLabelPasswordAgain);

        JButton btnNewButton = new JButton("提交");
        btnNewButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                //创建一个manager对象。
                SQLManager manager = SQLManager.newInstance();
                Statement statement = manager.getStatement();

                String userName = textFieldUserName.getText();
                //正则表达式判断用户名是否合法
                Pattern p_userName = Pattern.compile("^\\w+$");
                Matcher m_userName = p_userName.matcher(userName);
                boolean b_userName = m_userName.matches();


                String password = textFieldPassword.getText();
                //正则表达式判断密码是否合法
                Pattern p_password = Pattern.compile("^\\w+$");
                Matcher m_password = p_password.matcher(password);
                boolean b_password = m_password.matches();


                String passwordAgain = textFieldPasswordAgain.getText();
                //正则表达式判断密码是否合法
                Pattern p_passwordAgain = Pattern.compile("^\\w+$");
                Matcher m_passwordAgain = p_passwordAgain.matcher(passwordAgain);
                boolean b_passwordAgain = m_passwordAgain.matches();    
                if(b_userName){
                    System.out.println("输入的用户名合法!");
                    label_user.setText("");
                    if(b_password){
                        System.out.println("输入的密码合法!"); 
                        label_password.setText("");
                        if(b_passwordAgain){
                            System.out.println("输入的密码合法!");
                            label_passwordAgain.setText("");
                            try {
                                //判断该用户是否存在
                                String select ="select count(*) from user where name= '"+userName+"'";
                                ResultSet set =statement.executeQuery(select);
                                set.first();
                                int num = set.getInt(1);
                                if(num>0){
                                    System.out.println("该用户已经存在!");
                                }else{
                                    if(passwordAgain.equals(password))
                                    {
                                    String insert = "insert into user(name, password) values('"+userName+"','"+password+"')";
                                    statement.execute(insert);
                                    System.out.println("用户注册成功!");
                                    textFieldUserName.setText("");
                                    textFieldPassword.setText("");
                                    textFieldPasswordAgain.setText("");
                                    }
                                    else{
                                        System.out.println("输入的两次密码不一致!,请重新输入……");
                                    }

                                }

                            } catch (SQLException e1) {
                                e1.printStackTrace();
                            }

                        }else{
                            String warning_passwordAgain = "输入的密码不合法!";
                            System.out.println(warning_passwordAgain);
                            label_passwordAgain.setText(warning_passwordAgain);
                            textFieldPasswordAgain.setText("");
                        }               
                    }else{
                        String warning_password = "输入的密码不合法!";
                        label_password.setText(warning_password);
                        System.out.println(warning_password);
                        textFieldPassword.setText("");

                    }   
                }else{
                    String warning_user = "输入的用户名不合法!";
                    label_user.setText(warning_user);
                    System.out.println(warning_user);
                    textFieldUserName.setText("");
                }                                                                           
            }
        });
        btnNewButton.setBounds(149, 248, 93, 23);
        contentPane.add(btnNewButton);

        label_user = new JLabel("");
        label_user.setBounds(285, 80, 150, 15);
        contentPane.add(label_user);

        label_password = new JLabel("");
        label_password.setBounds(289, 123, 150, 15);
        contentPane.add(label_password);

        label_passwordAgain = new JLabel("");
        label_passwordAgain.setBounds(289, 178, 150, 15);
        contentPane.add(label_passwordAgain);

    }
    private JLabel label_user;
    private JLabel label_password ;
    private JLabel label_passwordAgain;
}

登录

功能实现

数据库中用户和密码的存储:
这里写图片描述

登录界面:
这里写图片描述

登录代码

//SQLManager类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

//采用单例设计模式
public class SQLManager {
    private Connection connection;
    private Statement statement;
    //获得statement
    public Statement getStatement() {
        return statement;
    }
    //赋值statement
    public void setStatement(Statement statement) {
        this.statement = statement;
    }
    //获得Connection
    public Connection getConnection() {
        return connection;
    }
    //赋值Connection
    public void setConnection(Connection connection) {
        this.connection = connection;
    }
    private static SQLManager manager;
    private SQLManager() {
        //连接数据库的驱动
                String driver = "com.mysql.jdbc.Driver";
                //指向数据库访问地址
                String url = "jdbc:mysql://localhost:3306/clazz";
                //数据库的用户名
                String user = "root";
                //数据库用户密码
                String password = "123456";

                try {
                    //加载驱动
                    Class.forName(driver);
                    //与数据库建立连接
                    connection = DriverManager.getConnection(url,user, password);
                    if(!connection.isClosed()){
                        //创建数据库操作类
                        statement = connection.createStatement();
                    }else{
                        System.out.println("请打开数据库!");
                    }

                } catch (ClassNotFoundException e) {            
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
    }

    /**
     * 创建该类的对象
     * @return 返回SQLManager对象。
     */
    public static synchronized SQLManager newInstance(){
        if(manager==null){
            manager = new SQLManager();
        }
        return manager;
    }
}
//登录
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.ActionEvent;

public class Login extends JFrame {

    private JPanel contentPane;
    private JTextField textUserName;
    private JTextField textPassword;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the frame.
     */
    public Login() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 451, 410);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblLogin = new JLabel("登录界面");
        lblLogin.setBounds(167, 57, 70, 31);
        contentPane.add(lblLogin);

        textUserName = new JTextField();
        textUserName.setBounds(153, 115, 137, 21);
        contentPane.add(textUserName);
        textUserName.setColumns(10);

        textPassword = new JTextField();
        textPassword.setBounds(153, 168, 137, 21);
        contentPane.add(textPassword);
        textPassword.setColumns(10);

        JButton btnNewButton = new JButton("登录");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String userName = textUserName.getText();
                String password = textPassword.getText();
                Connection conn = SQLManager.newInstance().getConnection();
                String sql = "select * from user where name= ? and password=? ";
                try {
                    PreparedStatement statement = conn.prepareStatement(sql);
                    statement.setString(1, userName);
                    statement.setString(2, password);
                    ResultSet set = statement.executeQuery();
                    set.last();//移动到最后一行。
                    int num = set.getRow();
                    if(num>0){
                        System.out.println(num);
                        System.out.println("用户登录成功!");
                        textUserName.setText("");
                        textPassword.setText("");
                    }else{
                        System.out.println(num);
                        System.out.println("用户登录失败!");
                        textUserName.setText("");
                        textPassword.setText("");
                    }               
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }       
            }
        });
        btnNewButton.setBounds(170, 246, 93, 23);
        contentPane.add(btnNewButton);

        JLabel lblUser = new JLabel("用户名:");
        lblUser.setBounds(55, 118, 54, 15);
        contentPane.add(lblUser);

        JLabel lblPassword = new JLabel("密码:");
        lblPassword.setBounds(55, 171, 54, 15);
        contentPane.add(lblPassword);
    }
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页