0
Follow
0
View

swing registration inserts the database

cuiping3210 注册会员
2023-02-25 20:12

Based on Monster group and GPT:
You can write the insert() method to insert data into the database as follows:

private void insert(){

    String username = enrolView.getUsernametext().getText();
    char[] chars = enrolView.getPasswordtext().getPassword();
    String password = new String(chars);

    Connection conn = null;
    PreparedStatement ps = null;

    try {
        //获取数据库连接
        conn = DBUtil.getConn();

        //插入数据的SQL语句
        String sql = "INSERT INTO users (`user_name`,`pwd`) values(?,?)";

        //创建PreparedStatement对象,并设置参数
        ps = conn.prepareStatement(sql);
        ps.setString(1, username);
        ps.setString(2, password);

        //执行插入操作
        int rows = ps.executeUpdate();

        //判断是否插入成功
        if (rows > 0) {
            JOptionPane.showMessageDialog(enrolView, "注册成功!");
        } else {
            JOptionPane.showMessageDialog(enrolView, "注册失败!");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        //关闭资源
        DBUtil.closePs(ps);
        DBUtil.closeConn(conn);
    }
}


In the insert() method above, we first get the username and password and store them in the username and password variables. We then get the database connection object by calling the DBUtil.getConn() method and INSERT the user name and password into the users table by performing an insert statement using the PreparedStatement object.

Note that we use the PreparedStatement object here to execute the SQL statement. The advantage of this is that SQL injection attacks are effectively avoided.

finally, we close the resource in the finally block by calling the DBUtil.closePs(ps) and DBUtil.closeConn(conn) methods.

jay926 注册会员
2023-02-25 20:12

< br / > zha full red elder brother

qklwdd1 注册会员
2023-02-25 20:12

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > according to provide you the code, you can try to implement the following method to insert the user data in the database:

private void insert(){
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DBUtil.getConn();
        String sql = "INSERT INTO users (`user_name`,`pwd`) values(?,?)";
        ps = conn.prepareStatement(sql);
        ps.setString(1, enrolView.getUsernametext().getText());
        ps.setString(2, new String(enrolView.getPasswordtext().getPassword()));
        int result = ps.executeUpdate();
        if (result > 0) {
            JOptionPane.showMessageDialog(enrolView, "注册成功!");
        } else {
            JOptionPane.showMessageDialog(enrolView, "注册失败!");
        }
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    } finally {
        DBUtil.closePs(ps);
        DBUtil.closeConn(conn);
    }
}


Where we used the DBUtil class to get a database connection and insert user data into the database. If the insertion is successful, the message "Registration succeeded!" is displayed. "Is displayed. Otherwise," Registration failed! "Dialog box. Note that we use the PreparedStatement class to handle the SQL statement when inserting data, avoiding the SQL injection problem.

In addition, you can call the insert() method in the sure() method to insert user data into the database, such as:

private void sure() {
    String username2 = enrolView.getUsernametext().getText();
    char[] chars = enrolView.getPasswordtext().getPassword();
    if (username2 == null || "".equals(username2.trim()) || chars == null) {
        JOptionPane.showMessageDialog(enrolView, "用户名密码必填");
        return;
    }

    // 检查用户名是否已被占用
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = DBUtil.getConn();
        String sql = "SELECT COUNT(*) FROM users WHERE user_name = ?";
        ps = conn.prepareStatement(sql);
        ps.setString(1, username2);
        rs = ps.executeQuery();
        if (rs.next() && rs.getInt(1) > 0) {
            JOptionPane.showMessageDialog(enrolView, "用户名已被占用!");
            return;
        }
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    } finally {
        DBUtil.closeRs(rs);
        DBUtil.closePs(ps);
        DBUtil.closeConn(conn);
    }

    insert();
}


In the sure() method, we first check that the username and password are empty. If the value is empty, the user name and password are Required dialog box is displayed, and then return. If neither the username nor the password is empty, we continue to check if the username is already occupied. If the user name is already in use, a message is displayed indicating that the user name is already in use. The prompt box and then return. Otherwise, we call the insert() method to insert user data into the database.

Note that in this process, we also use the ResultSet class to process the result set of the SQL query statement. Similarly, we need to close the ResultSet object at the end to avoid resource leakage.

About the Author

Question Info

Publish Time
2023-02-25 20:12
Update Time
2023-02-25 20:12