C.2 2023-02-25 21:59 采纳率: 41.2%
浏览 30
已结题

做项目注册功能的时候报错了

img



import com.roadjava.student.view.EnrolView;

import com.roadjava.entity.AdminDO;
import com.roadjava.service.AdminService;
import com.roadjava.service.impl.AdminServiceImpl;
import com.roadjava.student.view.LoginView;
import com.roadjava.student.view.MainView;
import com.roadjava.util.DBUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class EnrolHandler extends KeyAdapter implements ActionListener  {

    private  EnrolView enrolView;

    public EnrolHandler(EnrolView enrolView){this.enrolView = enrolView;}

    public void actionPerformed(ActionEvent e) {
        JButton Jbutton = (JButton) e.getSource();
        String text = Jbutton.getText();
        if ("确定".equals(text)) {
            sure();
        }

    }

    private void sure() {
        String username = enrolView.getUsernametext().getText();
        char[] chars = enrolView.getPasswordtext().getPassword();
        if (username == null || "".equals(username.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, username);
            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();

    }
    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);
        }
    }








}


package com.roadjava.student.view;

import com.roadjava.handler.EnrolHandler;
import com.roadjava.handler.LoginHandler;

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

public class EnrolView extends JFrame {
    JLabel username = new JLabel("用户名:");
    JLabel password = new JLabel("密码:");
    JTextField usernametext = new JTextField();
    JPasswordField passwordtext = new JPasswordField();
    JButton sure = new JButton("确定");

    EnrolHandler enrolHandler;


    public EnrolView() {


        enrolHandler = new EnrolHandler(this);

        setLayout(null);

        username.setFont(new Font("华文行楷", Font.PLAIN, 15));
        password.setFont(new Font("华文行楷", Font.PLAIN, 15));


        Container c1 = getContentPane();

        c1.add(username);
        c1.add(password);
        c1.add(usernametext);
        c1.add(passwordtext);
        c1.add(sure);


       sure.addActionListener(enrolHandler);
       sure.addKeyListener(enrolHandler);



        username.setBounds(0, 0, 200, 200);
        password.setBounds(0, 40, 200, 200);
        usernametext.setBounds(60, 90, 100, 20);
        passwordtext.setBounds(60, 130, 100, 20);
        sure.setBounds(50, 200, 80, 50);





        //设置默认按钮
        getRootPane().setDefaultButton(sure);








        setSize(200, 300);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);


    }

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

    public JTextField getUsernametext() {
        return usernametext;
    }

    public JPasswordField getPasswordtext() {
        return passwordtext;
    }
ackage com.roadjava.service.impl;

import com.roadjava.entity.AdminDO;
import com.roadjava.service.AdminService;
import com.roadjava.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class AdminServiceImpl implements AdminService {
    public boolean validateAdmin(AdminDO adminDO) {
        String userName = adminDO.getUserName();
        String pwdParam = adminDO.getPwd();
        String sql = "select pwd from user where user_name = ?";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;

        try {
            conn = DBUtil.getConn();
            if (conn == null) {
                return false;
            }
            ps = conn.prepareStatement(sql);
            ps.setString(1, userName);
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                String pwd = resultSet.getString(1);
                if (adminDO.getPwd().equals(pwd)) {
                    return true;
                }

            }

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            DBUtil.closeRs(resultSet);
            DBUtil.closeConn(conn);
            DBUtil.closePs(ps);

        }

        return false;
    }
}



public class AdminServiceImpl implements AdminService {
    public boolean validateAdmin(AdminDO adminDO) {
        String userName = adminDO.getUserName();
        String pwdParam = adminDO.getPwd();
        String sql = "select pwd from user where user_name = ?";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;

        try {
            conn = DBUtil.getConn();
            if (conn == null) {
                return false;
            }
            ps = conn.prepareStatement(sql);
            ps.setString(1, userName);
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                String pwd = resultSet.getString(1);
                if (adminDO.getPwd().equals(pwd)) {
                    return true;
                }

            }

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            DBUtil.closeRs(resultSet);
            DBUtil.closeConn(conn);
            DBUtil.closePs(ps);

        }

        return false;
    }
}



public class DBUtil {
    private static final String URL = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8&useSSl=false";
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String USER_NAME = "root";
    private static final String PWD = "lpb666666";

    static {
        try {
            //com.mysql.jdbc.Driver静态代码块
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
  //获取数据库连接
    public static Connection getConn() {
        try {
            return DriverManager.getConnection(URL, USER_NAME, PWD);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static void closeConn(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void closePs(PreparedStatement ps) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void closeRs(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}




  • 写回答

3条回答 默认 最新

  • pzzhao 2023-02-25 22:36
    关注

    你数据库中的表名应该是 user 吧,看你代码里又有user,又有users

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月5日
  • 已采纳回答 2月25日
  • 创建了问题 2月25日

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败