Home > Database > Mysql Tutorial > body text

How to use Java and MySQL database to implement registration and login

PHPz
Release: 2023-06-01 08:49:05
forward
1672 people have browsed it

Design ideas

1. Database design - after understanding the user's needs, design the database according to the user's needs.

2. Data model - it is to encapsulate all the structure of the database table in a class.

3. Data processing tool class - Design a tool class to process data according to the requirements given by the data model.

4. View display - finally provide users with an interactive and convenient view interface.

Detailed design

1. First, create a table in the database.

2. Develop corresponding data models based on business needs. This design mainly focuses on login and registration, so two data models are designed.

(1) Registration model

package Chapter_14.Part_8;

public class Register {	//注册模型
    String id;
    String password;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
Copy after login

(2) Login model

package Chapter_14.Part_8;

public class Login {	//登录模型
    String id;
    String password;
    boolean loginSuccess = false;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isLoginSuccess() {
        return loginSuccess;
    }

    public void setLoginSuccess(boolean loginSuccess) {
        this.loginSuccess = loginSuccess;
    }
}
Copy after login

3. Based on the two data models designed above, design corresponding data processing tools respectively. kind.

(1) Register model processing tool class

package Chapter_14.Part_8;

import Chapter_14.GetDatabaseConnection;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class registerHandle {	//注册数据处理者
    Connection connection = null;
    PreparedStatement presql;   //预处理对象
    String sql;

    public registerHandle() {
        connection = GetDatabaseConnection.connectDB("book","root","");
    }
    public void writeRegister(Register register){
        int ok = 0;
        try {
            sql = "insert into register value (?,?)";    //预处理
            presql = connection.prepareStatement(sql);
            presql.setString(1,register.getId());
            presql.setString(2,register.getPassword());
            ok = presql.executeUpdate();    //成功插入式返回1
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null,"ID不能重复","警告",JOptionPane.WARNING_MESSAGE);

        }
        if (ok!=0){
            JOptionPane.showMessageDialog(null,"注册成功","恭喜",JOptionPane.WARNING_MESSAGE);
        }
    }
}
Copy after login

(2) Log in model processing tool class

package Chapter_14.Part_8;

import Chapter_14.GetDatabaseConnection;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class loginHandle {
    Connection connection = null;
    PreparedStatement presql;
    String sql;
    ResultSet resultSet;
    boolean loginSuccess;

    public loginHandle() {
        connection = GetDatabaseConnection.connectDB("book", "root", "");
    }

    public void readLogin(Login login) {
        try {
            sql = "select * from register where ID = ? and password = ? ";  //主要用and
            presql = connection.prepareStatement(sql);
            presql.setString(1, login.getId());
            presql.setString(2, login.getPassword());
            resultSet = presql.executeQuery();
            if (resultSet.next()) {
                login.setLoginSuccess(true);
                JOptionPane.showMessageDialog(null, "登录成功", "恭喜", JOptionPane.WARNING_MESSAGE);
            } else {
                login.setLoginSuccess(false);
                JOptionPane.showMessageDialog(null, "登录失败", "提示", JOptionPane.WARNING_MESSAGE);
            }
            connection.close();
            loginSuccess = login.isLoginSuccess();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
Copy after login

4. You can design a test class to verify the program’s ability passed normally. If it passes, you can start designing the view.

(1) Design of registration view

package Chapter_14.Part_8;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class registerPanel extends JPanel implements ActionListener {
    Register register;
    registerHandle registerHandle;
    JTextField jTextFieldID;
    JTextField jTextFieldPassword;
    JButton jregister;

    public registerPanel() {
        //setLayout(new FlowLayout());
        //setVisible(true);
        init();
        validate();
    }

    void init() {
        jTextFieldID = new JTextField(12);
        add(new JLabel("ID:"));
        add(jTextFieldID);
        jTextFieldPassword = new JTextField(12);
        jTextFieldPassword.addActionListener(this);
        add(new JLabel("PASSWORD:"));
        add(jTextFieldPassword);
        jregister = new JButton("Register");
        jregister.addActionListener(this);
        add(jregister);


    }


    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        register = new Register();
        register.setId(jTextFieldID.getText());
        register.setPassword(jTextFieldPassword.getText());
        registerHandle = new registerHandle();
        registerHandle.writeRegister(register);
        jTextFieldPassword.setText(null);
    }
}
Copy after login

(2) Design of login view

package Chapter_14.Part_8;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class loginPanel extends JPanel implements ActionListener {
    Login login;
    loginHandle loginHandle;
    JTextField jTextFieldID;
    JPasswordField jTextFieldPassword;
    JButton jLogin;

    public loginPanel() {
        //setLayout(new FlowLayout());
        //setVisible(true);
        init();
        validate();
    }

    void init() {
        jTextFieldID = new JTextField(12);
        add(new JLabel("ID:"));
        add(jTextFieldID);
        jTextFieldPassword = new JPasswordField(12);
        jTextFieldPassword.addActionListener(this);
        add(new JLabel("PASSWORD:"));
        add(jTextFieldPassword);
        jLogin = new JButton("Login");
        jLogin.addActionListener(this);
        add(jLogin);
    }

    public boolean isLoignSuccess() {
        return loginHandle.loginSuccess;
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        login = new Login();
        login.setId(jTextFieldID.getText());
        char[] p = jTextFieldPassword.getPassword();
        login.setPassword(new String(p));
        loginHandle = new loginHandle();
        loginHandle.readLogin(login);
    }
Copy after login

5. Combine the above two view classes into one class for convenience later Maintenance and modification.

package Chapter_14.Part_8;

import javax.swing.*;
import java.awt.*;

public class combinePanel extends JPanel {
    JTabbedPane jTabbedPane;
    registerPanel registerPanel;
    loginPanel loginPanel;

    public combinePanel() {
        //setLayout(new BorderLayout());    不设置时不全框显示
        jTabbedPane = new JTabbedPane();
        registerPanel = new registerPanel();
        loginPanel = new loginPanel();
        jTabbedPane.addTab("注册界面", registerPanel);
        jTabbedPane.addTab("登录界面", loginPanel);
        add(jTabbedPane, BorderLayout.CENTER);
        jTabbedPane.validate();
    }

    public boolean isLoginSuccess() {
        return loginPanel.isLoignSuccess();
    }
}
Copy after login

6. After everything is designed, it can be run. Set the main entrance of a program.

package Chapter_14.Part_8;

public class Example14_9 {
    public static void main(String[] args) {
        myWindow myWindow = new myWindow();
    }
}
Copy after login

The above is the detailed content of How to use Java and MySQL database to implement registration and login. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template