Java期末大作业,基于gui,mysql实现的简易用户管理系统

废话不多少先看效果图,决定一下你是否需要继续看下去


登录界面


在这里插入图片描述

管理界面


在这里插入图片描述

注册界面


在这里插入图片描述

登录页面介绍


页面比较简单,就两个文本框,两个按钮,登录验证只有账号密码匹不匹配,其他的没做验证,比如用户名不能出现数字啥的

管理页面介绍


只要数据库存的有账号,都可以实现登录,出现管理页面,下方的表格会输出数据库中所有用户信息,鼠标点击一条信息,上方相应文本框会获得数据,进行增删改后都会清空文本框,当重复添加用户时会弹出警告窗口
在这里插入图片描述

注册页面介绍


注册页面有简单的验证,比如用户名不可为空,用户名的长度大于2小于10等,不过验证是粗粒度的,比如用户名不能为数字,年龄不能为字符串等,如果有需要的可自行扩展,注册成功会提示,然后可点击取消回到登录页面
在这里插入图片描述
在这里插入图片描述

可以看到注册信息被写入数据库

在这里插入图片描述

接下来上代码,不要问我为啥放在最后面才上代码,我也不晓得

  • User
package com.zjx.manage;

/**
 * User-用来接收从数据库中查询到的数据
 * @author zjx
 * @create 2021-11-21 14:39
 */
public class User {

    private String root;
    private String password;
    private int age;
    private String address;

    public User() {
    }

    public User(String root, String password, int age, String address) {
        this.root = root;
        this.password = password;
        this.age = age;
        this.address = address;
    }

    public String getRoot() {
        return root;
    }

    public void setRoot(String root) {
        this.root = root;
    }

    public String getPassword() {
        return password;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "root='" + root + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }

    public static void main(String[] args) {
        User user = new User();
        System.out.println(user);
        System.out.println("===============");
        String str = "";
        int aa = Integer.parseInt(str);
        System.out.println(aa);
    }
}

  • jdbc.properties–这玩意跟User啥的放在一个包下,我这里是manage
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatement=true
username=root
password=123456

#初始化时物理连接个数
initialSize=10
#最大连接池数量
maxActive=15
#最小连接池数量
minIdle=10
  • JDBCUtils
package manage;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

/**
 * 这是一个工具类,用来连接数据库的
 * 使用druid数据库连接池
 * @author zjx
 * @create 2021-11-21 14:09
 */
public class JDBCUtils {
    private static DataSource dataSource;
    static {
        //用来加载配置文件的
        Properties prop2 = new Properties();
        try {
            //读取配置文件
            InputStream fis = JDBCUtils.class.getResourceAsStream("druid.properties");
            prop2.load(fis);
            //创建数据库连接池对象
            dataSource = DruidDataSourceFactory.createDataSource(prop2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过数据库连接池获取连接
     * @return 连接对象
     * @throws Exception
     */
    public static Connection getDruidConnection() throws Exception{
        Connection con = dataSource.getConnection();
        return con;
    }
}
  • SQLQuery–读写数据库的工具类
package com.zjx.manage;


import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.util.List;

/**
 * @author zjx
 * @create 2021-11-21 14:37
 */
public class SQLQuery {

    private String sql;
    private String username;
    private String password;
    private int age;
    private String address;
    private QueryRunner runner = new QueryRunner();
    /**
     * 向数据库中添加一个user
     * @param user 要添加的user
     * @return 返回添加结果,受影响行数,大于0表示SQL语句执行成功
     * @throws Exception 异常信息
     */
    public int addUser(User user) {
        int add = 0;
        try {
            Connection connection = JDBCUtils.getDruidConnection();
            sql = "insert into manage(root, password, age, address) values(?, ?, ?, ?)";
            username = user.getRoot();
            password = user.getPassword();
            age = user.getAge();
            address = user.getAddress();
            add = runner.update(connection, sql, username, password, age, address);

            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return add;
        }

    }

    /**
     * 删除一条用户记录
     * @param user 待删除用户信息
     * @return 删除结果,大于0表示删除成功
     * @throws Exception
     */
    public int deleteUser(User user) throws Exception {
        Connection connection = JDBCUtils.getDruidConnection();
        sql = "delete from manage where root = ?";
        int update = runner.update(connection, sql, user.getRoot());

        connection.close();
        return update;
    }

    /**
     * 修改用户记录
     * @param brfore 修改前用户信息
     * @param after 修改后用户信息
     * @return 修改结果,大于0表示修改成功
     * @throws Exception
     */
    public int updateUser(User brfore, User after) throws Exception {
        Connection connection = JDBCUtils.getDruidConnection();
        sql = "update manage set root = ?, password = ?, age = ?, address = ? where username = ?";
        int update = runner.update(connection, sql, after.getRoot(), after.getPassword(), after.getAge(),
                after.getAddress(), brfore.getRoot());

        connection.close();

        return update;
    }

    /**
     * 查询数据库中所有用户信息
     * @return List<User> 集合存放所有用户
     * @throws Exception
     */
    public List<User> queryForAll() throws Exception {
        Connection connection = JDBCUtils.getDruidConnection();
        sql = "select * from manage";
        List<User> query = runner.query(connection, sql, new BeanListHandler<User>(User.class));

        connection.close();
        return query;
    }

    /**
     * 根据用户名查询用户信息
     * @param username 用户名
     * @return 封装用户信息的javaBean
     * @throws Exception
     */
    public User queryForOne(String username) {
        User user = null;
        try {
            Connection connection = JDBCUtils.getDruidConnection();
            sql = "select * from manage where root = ?";
            BeanHandler<User> handler = new BeanHandler<>(User.class);
            user = runner.query(connection, sql, handler, username);

            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return user;
        }

    }

}


  • StartMysql–程序启动类
package manage;

/**
 * @author zjx
 * @create 2021-11-21 12:29
 */
public class StartMysql {
    // 启动登录界面
    public static void main(String[] args) {
        new Login().init();
    }
}
  • Login–登录页面
package com.zjx.manage;


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

/**
 * @author zjx
 * @create 2021-11-21 14:21
 */
public class Login {
    private final JFrame jf = new JFrame("用户管理系统登录_ZJX");
    final int WIDTH = 500;
    final int HEIGHT = 300;

    //用来跟数据库交互,进行增删改查
    private SQLQuery sqlQuery = new SQLQuery();
    private String username;
    private String password;


    private JTextField uField;
    private JTextField pField;
    public void init() {
        try {
            //获取登录界面
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            int sw = screenSize.width;//获取屏幕的宽
            int sh = screenSize.height;//获取屏幕的高
            //设置界面居中显示
            jf.setBounds((sw - WIDTH) / 2, (sh - HEIGHT) / 2, WIDTH, HEIGHT);
            jf.setResizable(false);//生成的窗口大小不可更改
            //获取resources路径下的文件
//            InputStream stream = Resources.class.getResourceAsStream("/img/login.png");
            //JFrame设置icon
//            jf.setIconImage(ImageIO.read(stream));
            //修改为FlowLayout布局,否则窗口中的组件会根据窗口大小变化尺寸
            jf.setLayout(new FlowLayout());
            //添加组件
            Box vBox = Box.createVerticalBox();
            Box uBox = Box.createHorizontalBox();
            JLabel uLabel = new JLabel("用户名:");
            //用户名输入框
            uField = new JTextField(15);
            //在布局中添加组件,并使用占位符(以下同理)
            uBox.add(uLabel);
            uBox.add(Box.createHorizontalStrut(20));
            uBox.add(uField);
            Box pBox = Box.createHorizontalBox();
            JLabel pLabel = new JLabel("密    码:");
            //密码输入框
            pField = new JTextField(15);
            pBox.add(pLabel);
            pBox.add(Box.createHorizontalStrut(20));
            pBox.add(pField);
            Box btnBox = Box.createHorizontalBox();
            //添加button和监听事件
            JButton loginBtn = new JButton("登录");
            loginBtn.setName("loginBtn");
            loginBtn.addActionListener(new MyActionListener());
            JButton registerBtn = new JButton("注册");
            registerBtn.setName("registerBtn");
            registerBtn.addActionListener(new MyActionListener());
            btnBox.add(loginBtn);
            btnBox.add(Box.createHorizontalStrut(80));
            btnBox.add(registerBtn);

            vBox.add(Box.createVerticalStrut(50));
            vBox.add(uBox);
            vBox.add(Box.createVerticalStrut(12));
            vBox.add(pBox);
            vBox.add(Box.createVerticalStrut(50));
            vBox.add(btnBox);
            jf.add(vBox);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jf.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //自定义监听类
    private class MyActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton button = (JButton) e.getSource();
            String name = button.getName();

            // 点击登录按钮后
            if ("loginBtn".equals(name)) {
                username = uField.getText();
                password = pField.getText();
                User user = null;
                try {
                    user = sqlQuery.queryForOne(username);

                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                if (user != null && user.getPassword().equals(password)) {
                    System.out.println("用户名密码匹配正确,可以登录");
                    jf.dispose();
                    new MySQLGUI();
                } else {
                    JOptionPane.showMessageDialog(null, "用户名或密码错误", "错误警告",JOptionPane.ERROR_MESSAGE);
                }
            } else if ("registerBtn".equals(name)) {
                jf.dispose();
                new regiest();
            }
        }
    }

}
  • MySQLGUI–用户信息管理页面
package com.zjx.manage;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

/**
 * @author zjx
 * @create 2021-11-21 12:31
 */
public class MySQLGUI extends JFrame {

    JLabel j11=new JLabel("编号                      ");
    JLabel j12=new JLabel("姓名                      ");
    JLabel j13=new JLabel("年龄                      ");
    JLabel j14=new JLabel("地址                      ");

    JPanel jp2=new JPanel();  //表头
    JLabel j1=new JLabel("编号");
    JLabel j2=new JLabel("姓名");
    JLabel j3=new JLabel("年龄");
    JLabel j4=new JLabel("地址");
    JLabel j5=new JLabel("密码");

    JTextField f1=new JTextField(10);
    JTextField f2=new JTextField(10);
    JTextField f3=new JTextField(10);
    JTextField f4=new JTextField(30);
    JTextField f5=new JTextField(10);

    JButton b2=new JButton("修改");
    JButton b3=new JButton("删除");
    JButton b4=new JButton("清空");
    JButton b5=new JButton("添加");

    final JTable t1=new JTable();
    private User before = null;
    private String[][] datas = {};
    private List<User> users = null;
    private SQLQuery sqlQuery = new SQLQuery();
    private DefaultTableModel model;


    //设置表格列宽
    public void setTableRowAndCol(int width1, int width2, int width3, int width4) {
        //设置表格列宽
        t1.getColumnModel().getColumn(0).setPreferredWidth(width1);
        t1.getColumnModel().getColumn(1).setPreferredWidth(width2);
        t1.getColumnModel().getColumn(2).setPreferredWidth(width3);
        t1.getColumnModel().getColumn(3).setPreferredWidth(width4);
    }

    //清空文本框数据
    public void cleanAll() {
        f1.setText("");
        f2.setText("");
        f3.setText("");
        f4.setText("");
        f5.setText("");
    }
    public  MySQLGUI() {

        this.setTitle("用户管理");
        this.setLayout(null);
        this.setLocation(300,100 );
        this.setSize(850,600 );
        this.setLayout(null);
        //支持滚动
        JScrollPane scrollPane = new JScrollPane(t1);
        JPanel panel = new JPanel();
        JPanel panel2 = new JPanel();
        panel.add(t1);
        panel.setBounds(50, 130,800, 1000);

        jp2.setBounds(-80, 100, 800, 100); //表头



        this.add(panel);
        final String[] titles = new String[]{"编号", "姓名", "年龄", "地址"};

        try {
            users = new SQLQuery().queryForAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        model=new DefaultTableModel(datas, titles);
        t1.setModel(model);
        if(t1.getRowCount()>0)
        {
            t1.setRowSelectionInterval(0, 0);
        }
        //将数据库中所有的信息打印出来
        for (int i = 0; i < users.size(); i++) {
            System.out.println("=======开始打印第"+i+"条数据========");
            System.out.println("user["+i+"] = "+ users.get(i));
            String username = users.get(i).getRoot();
            System.out.println("username = "+username);
            String age = ""+users.get(i).getAge();
            System.out.println("age = "+age);
            String address = users.get(i).getAddress();
            System.out.println("address = "+address);
            model.addRow(new String[]{""+i, username, age, address});
        }
        System.out.println("=======结束打印========");


        t1.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                System.out.println("鼠标点击");
                // TODO 自动生成的方法存根
                int row = t1.getSelectedRow();
                String id = t1.getValueAt(row, 0).toString();
                System.out.println("id = "+id);
                String username = t1.getValueAt(row, 1).toString();
                System.out.println("username = "+username);
                String age = t1.getValueAt(row, 2).toString();
                System.out.println("age = "+age);
                String address = t1.getValueAt(row, 3).toString();
                System.out.println("adress = "+address);
                try {
                    before = new SQLQuery().queryForOne(username);
                } catch (Exception exception) {
                    exception.printStackTrace();
                }

                f1.setText(id);
                f2.setText(username);
                f3.setText(age);
                f4.setText(address);
                String password = null;
                try {
                    password = new SQLQuery().queryForOne(username).getPassword();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                f5.setText(password);

            }
        });
        b2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("修改按钮被触发");
                // TODO 自动生成的方法存根
                String username=f2.getText().trim();
                int age=Integer.valueOf(f3.getText().trim());
                if (age > 150) {
                    JOptionPane.showMessageDialog(null, "输入的年龄不合规范!", "操作错误",JOptionPane.ERROR_MESSAGE);
                    return ;
                }
                String address=f4.getText().trim();
                String password=f5.getText().trim();

                User after = new User(username, password, age, address);

                try {
                    new SQLQuery().updateUser(before, after);
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                cleanAll();
                //更新完数据重新从数据库中读取,并打印
                try {
                    users = sqlQuery.queryForAll();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }

                //重新绘制表格
                model = new DefaultTableModel(datas, titles);
                t1.setModel(model);
                if(t1.getRowCount()>0)
                {
                    t1.setRowSelectionInterval(0, 0);
                }

                //设置表格的列宽
                setTableRowAndCol(100, 100, 100, 400);
                //将数据库中所有的信息打印出来
                for (int i = 0; i < MySQLGUI.this.users.size(); i++) {
                    System.out.println("=======开始打印第"+i+"条数据========");
                    String username2 = MySQLGUI.this.users.get(i).getRoot();
                    String age2 = ""+ MySQLGUI.this.users.get(i).getAge();
                    String address2 = MySQLGUI.this.users.get(i).getAddress();

                    model.addRow(new String[]{""+i, username2, age2, address2});
                }
            }
        });
        b3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO 自动生成的方法存根
                String username = f2.getText().trim();
                int age = Integer.valueOf(f3.getText().trim());
                String address = f4.getText().trim();
                String password = f5.getText().trim();
                User user = new User(username, password, age, address);
                try {
                    new SQLQuery().deleteUser(user);
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                try {
                    users = sqlQuery.queryForAll();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                // 删除用户信息清空文本框数据
                cleanAll();
                //重新绘制表格
                model = new DefaultTableModel(datas, titles);
                t1.setModel(model);
                if(t1.getRowCount()>0)
                {
                    t1.setRowSelectionInterval(0, 0);
                }

                //设置表格的列宽
                setTableRowAndCol(100, 100, 100, 400);

                //将数据库中所有的信息打印出来
                for (int i = 0; i < users.size(); i++) {
                    System.out.println("=======开始打印第"+i+"条数据========");
                    String username2 = users.get(i).getRoot();
                    String age2 = ""+users.get(i).getAge();
                    String address2 = users.get(i).getAddress();

                    model.addRow(new String[]{""+i, username2, age2, address2});
                }
            }
        });
        b4.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO 自动生成的方法存根
                //清除文本框数据
                cleanAll();
            }
        });
        b5.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO 自动生成的方法存根

                String username = f2.getText().trim();
                int age = Integer.valueOf(f3.getText().trim());
                String address = f4.getText().trim();
                String password = f5.getText().trim();

                User user = new User(username, password, age, address);
                try {
                    new SQLQuery().addUser(user);
                } catch (Exception exception) {
                    JOptionPane.showMessageDialog(null, "该用户信息已存在,不可重复添加");
                    cleanAll();
                    return ;
                }
                try {
                    users = sqlQuery.queryForAll();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                cleanAll();
                //重新绘制表格
                model = new DefaultTableModel(datas, titles);
                t1.setModel(model);
                if(t1.getRowCount()>0)
                {
                    t1.setRowSelectionInterval(0, 0);
                }

                //设置表格的列宽
                setTableRowAndCol(100, 100, 100, 400);

                //将数据库中所有的信息打印出来
                for (int i = 0; i < users.size(); i++) {
                    System.out.println("=======开始打印第"+i+"条数据========");
                    String username2 = users.get(i).getRoot();
                    String age2 = ""+users.get(i).getAge();
                    String address2 = users.get(i).getAddress();

                    model.addRow(new String[]{""+i, username2, age2, address2});
                }
            }
        });
        jp2.add(j11);   //第一个表头
        jp2.add(j12);
        jp2.add(j13);
        jp2.add(j14);

        this.add(jp2);
        panel2.setBounds(20,20,780,100);

        j5.setBounds(-100, 41, 30, 20);
        panel2.add(j1);
        panel2.add(f1);
        panel2.add(j2);
        panel2.add(f2);
        panel2.add(j3);
        panel2.add(f3);
        panel2.add(j4);
        panel2.add(f4);
        panel2.add(j5);
        panel2.add(f5);
        panel2.add(b2);
        panel2.add(b3);
        panel2.add(b4);
        panel2.add(b5);
        //设置表格的列宽
        setTableRowAndCol(100, 100, 100, 400);
        this.setResizable(false); //设置不可以变大
        this.add(panel2);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new MySQLGUI();
    }
}

  • regiest–注册页面
package com.zjx.manage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Pattern;

/**
 * 注册页面
 * @author zjx
 * @create 2021-11-21 21:32
 */
public class regiest extends JFrame {

    private static final long serialVersionUID = -6788045638380819221L;
    private JTextField age;//年龄
    private JTextField uname;//用户名
    private JPasswordField pwd;//密码
    private JPasswordField confirmPwd;//确认密码


    private JTextField address;//地址

    //小容器
    private JLabel jTitle;
    private JLabel j1;
    private JLabel j2;
    private JLabel j3;
    private JLabel j4;
    private JLabel j6;
    private JLabel j7;
    private JLabel j8;
    private JLabel j14;
    private JLabel j16;
    //小按钮
    private JButton b2;
    private JButton b3;

    private ButtonGroup bg;

    /**
     * 注册UI的构造
     * */
    public regiest() {
        //设置登录窗口标题
        this.setTitle("用户注册");
        //采用指定的窗口装饰风格
        this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
        //窗体组件初始化
        init();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置布局为绝对定位
        this.setLayout(null);
        this.setBounds(0, 0, 355, 580);
        //设置窗体的图标
        Image img0 = new ImageIcon("").getImage();
        this.setIconImage(img0);
        //窗体大小不能改变
        this.setResizable(false);
        //居中显示
        this.setLocationRelativeTo(null);
        //窗体显示
        this.setVisible(true);
    }
    /**
     * 窗体组件初始化
     * */
    public void init() {
        //创建一个容器,其中的图片大小和setBounds第三、四个参数要基本一致(需要自己计算裁剪)
        Container container = this.getContentPane();
        j1 = new JLabel();
        //设置背景色
        Image img1 = new ImageIcon("").getImage();
        j1.setIcon(new ImageIcon(img1));
        j1.setBounds(0, 0, 355, 540);

        j2 = new JLabel();
        Image img2 = new ImageIcon("").getImage();
        j2.setIcon(new ImageIcon(img2));
        j2.setBounds(40, 95, 50, 53);

        jTitle = new JLabel("用户注册");
        jTitle.setBounds(140, 10, 150, 53);
        jTitle.setForeground(new Color(208, 106, 92));
        jTitle.setFont(new Font("微软雅黑", Font.BOLD, 20));

        bg = new ButtonGroup();

        uname = new JTextField();//用户名输入框
        uname.setBounds(100, 150, 150, 20);
        j7 = new JLabel("用户名");//用户名标签
        j7.setBounds(60, 150, 250, 20);

        age = new JTextField();//年龄输入框
        age.setBounds(100, 180, 150, 20);
        j6 = new JLabel("年"+" "+"龄");//年龄标签
        j6.setBounds(60, 180, 250, 20);

        pwd = new JPasswordField();//密码输入框
        pwd.setBounds(100, 210, 150, 20);
        j8 = new JLabel("密"+" "+"码");//密码标签
        j8.setBounds(60, 210, 250, 20);

        address = new JTextField();//地址输入框
        address.setBounds(100, 240, 150, 20);
        j16 = new JLabel("地址");//地址标签
        j16.setBounds(60, 240, 250, 20);

        //友情提示框
        j14 = new JLabel();
        j14.setBounds(60, 480, 250, 20);

        //注册按钮
        b2 = new JButton("注册");
        b2.setBounds(70, 520, 75, 20);
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String cmd = e.getActionCommand();
                Pattern pattern = Pattern.compile("[0-9]*");//正则表达式 数字校验
                User user = new User();

                String setage = age.getText().equals("") ? "0": age.getText();
                user.setAge(Integer.parseInt(setage));
                user.setRoot(uname.getText());
                user.setPassword(new String(pwd.getPassword()));
                user.setAddress(address.getText());
                String msg = RegistService.regist(user);

                j14.setText("系统友情提示:" + msg);
                j14.setForeground(Color.RED);
            }
        });
        //取消按钮
        b3 = new JButton("取消");
        b3.setBounds(205, 520, 65, 20);

        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                regiest.this.dispose();
                new Login().init();
            }
        });

        //所有组件用容器装载
        j1.add(jTitle);
        j1.add(j2);
        j1.add(j6);
        j1.add(j7);
        j1.add(j8);
        j1.add(j14);
        j1.add(j16);
        j1.add(b2);
        j1.add(b3);
        container.add(j1);
        container.add(age);
        container.add(uname);
        container.add(pwd);
        container.add(address);
    }

}

  • RegistService–验证规则
package com.zjx.manage;


import java.util.regex.Pattern;

/**
 * 注册方法
 * @author zjx
 * @create 2021-11-21 21:43
 */
public class RegistService{

    /**
     * 注册方法
     */
    public static String regist(User user){
        //正则表达式 数字校验
        Pattern pattern = Pattern.compile("[0-9]*");


        String name = user.getRoot();
        if ("".equals(name)){
            return "请输入姓名!";
        }
        if (name.length()>10 || name.length() < 2){
            return "姓名长度不合法!";
        }

        int age = user.getAge();
        if (age >= 120){
            return "年龄不符合规范";
        }
        String pwd = user.getPassword();
        if ("".equals(pwd)){
            return "请输入密码!";
        }
        if (pwd.length()>15 || pwd.length()<6){
            return "密码长度不正确!";
        }

        //判断用户名有没有被使用
        User user1 = null;


        user1 = new SQLQuery().queryForOne(user.getRoot());

        if (user1 != null) {
            return "用户名已存在!";
        }
        new SQLQuery().addUser(user);
        return "恭喜你,注册成功!";
    }
}
欧克,大功告成

写的这么详细,不点个赞再走嘛

  • 41
    点赞
  • 145
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 16
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

star_zhang_jx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值