(1)在数据库stuDB中建立user表(username,psw)(2)建立用户界面如图(3)完成登录功能,根据用户名查询,用户名和密码从数据库中读出,进行验证。(不考虑多线程的问题)

(1)在数据库stuDB中建立user表(username,psw)

(2)建立用户界面如图

(3)完成登录功能,根据用户名查询,用户名和密码从数据库中读出,进行验证。(不考虑多线程的问题)

例图: 

 主函数测试类

public class demo {
    public static void main(String[] args) throws Exception {
        loginJFrame j2=new loginJFrame();
    }
}

 用户类:方便管理信息。

import java.util.Objects;
public class User {
    private String name;
    private String password;

    public User() {
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return password
     */
    public String getPassword() {
        return password;
    }

    /**
     * 设置
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    public String toString() {
        return "User{name = " + name + ", password = " + password + "}";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(name, user.name) && Objects.equals(password, user.password);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, password);
    }
}

 主体类:实现界面功能,和主体函数逻辑,和数据库进行连接以及读取用户信息等等。。。

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.ArrayList;

public class loginJFrame extends JFrame implements ActionListener {
    ArrayList<User> list=new ArrayList<>();
    private  JLabel label1;
    private  JLabel label2;
    private  JLabel label3;
    private  JTextField text1;
    private  JTextField text2;
    private JButton button1;
    private JButton button2;
    public loginJFrame() throws Exception {
        this.setSize(500,560);

        this.setTitle("登录界面");

        //this.setAlwaysOnTop(true);

        this.setLocationRelativeTo(null);

        this.setDefaultCloseOperation(3);

        this.setResizable(false);

        this.readUserInf();

        this.init();

        this.setVisible(true);
    }

    private void readUserInf() throws Exception {
        String url="jdbc:mysql://localhost:3306/stu";
        String user="root";
        String pwd="123456";
        Connection connection = DriverManager.getConnection(url,user,pwd);
        Statement statement = connection.createStatement();
        String sql="select * from user";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()){
            String name = resultSet.getString("name");
            String password = resultSet.getString("password");
            list.add(new User(name,password));
        }
        statement.close();
        connection.close();
    }

    public void init(){

        label2=new JLabel("账号");
        label2.setBounds(40,200,79,17);
        this.getContentPane().add(label2);

        text1=new JTextField(300);
        text1.setBounds(150,200,300,30);
        this.getContentPane().add(text1);


        label3=new JLabel("密码");
        label3.setBounds(40,300,79,17);
        this.getContentPane().add(label3);

        text2=new JTextField(300);
        text2.setBounds(150,300,300,30);
        this.getContentPane().add(text2);


        button1=new JButton("登录");
        button1.setBounds(60,380,128,47);
        button1.addActionListener(this);
        this.getContentPane().add(button1);


        button2=new JButton("注册");
        button2.setBounds(300,380,128,47);
        button2.addActionListener(this);
        this.getContentPane().add(button2);


        ImageIcon image5=new ImageIcon("C:\\Users\\略略略\\IdeaProjects\\out\\src\\puzzlegame\\image\\login\\background.png");
        label1=new JLabel(image5);
        this.getContentPane().add(label1);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==button1){
            String name=text1.getText();
            String password=text2.getText();
            if(list.contains(new User(name,password))){
                JOptionPane.showMessageDialog(null, "信息正确,登录成功", "Title",JOptionPane.PLAIN_MESSAGE);
            } else{
                JOptionPane.showMessageDialog(null, "信息错误,登录失败", "Title",JOptionPane.PLAIN_MESSAGE);
            }

        }
        if(e.getSource()==button2){
                String name=text1.getText();
                String password =text2.getText();
                User u=new User(name,password);
                if(list.contains(u)){
                    //数据库已经存在数据,弹出会话窗提示
                    JOptionPane.showMessageDialog(null, "已注册!", "Title",JOptionPane.PLAIN_MESSAGE);
                }
                else{
                    //数据库还没有存在该数据,向数据库写入数据
                    try {
                        mysqlAdd(name,password);
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                    list.add(new User(name,password));
                    JOptionPane.showMessageDialog(null, "注册成功!", "Title",JOptionPane.PLAIN_MESSAGE);
                }

        }
    }

    public void mysqlAdd(String name, String password) throws SQLException {
        //Class.forName("com.mysql.cj.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/stu";
        String user="root";
        String pwd="123456";
        Connection connection = DriverManager.getConnection(url,user,pwd);
        Statement statement = connection.createStatement();
        String sql="insert into user ( name, password) values ('"+name+"', '"+password+"')";
        statement.executeLargeUpdate(sql);
        statement.close();
        connection.close();
    }
}

 数据库事先写入数据:

 

 界面展示:

用户可以连接数据库实现登录和注册的功能。

登录:

账号密码正确--登录成功。

账号密码错误--登录失败。

注册:

账号密码存在--已存在,注册失败。

账号密码不存在--注册成功!

 

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值