MySQL 将参数从 JSP 页面传递到连接到数据库的 servlet 时,JSP 项目中的异常列名称不能为空

MySQL exception column name can't be null on JSP project when passing parameters from JSP page to servlet connected to a database

首先,我是 JSP 的新手,正在尝试一些项目

我有一个 jsp 表单可以提交到数据库,这里是表单代码:

                        ...<form action="process/insert_category" method="post">
                            <div class="row" >
                                <div class="col-12">
                                    <h5 class="form-title"><span>Book-Category Information</span></h5>
                                </div>

                                 <div class="col-12 col-sm-6">
                                    <div class="form-group">
                                        <label>Category Name</label>
                                        <input type="text" name="categoryname" class="form-control">
                                    </div>
                                </div>
                                <div class="col-12">
                                    <button type="submit" class="btn btn-primary">Submit</button>
                                </div>
                            </div>
                        </form>...

对于类别名称,我必须获取 name=categoryname 作为参数提交到后端

这是 servlet 代码:

protected void InsertCategory(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {

            String categoryname = request.getParameter("categoryname");

            Category category = new Category(categoryname);
            if (!(ada.checkCategory(categoryname))) {
                if (ada.insert_category(category) > 0) {
                    request.setAttribute("success", "Category Added Successfully");
                    response.sendRedirect("../book-categories");

                } else {
                    request.setAttribute("error", "Category Not Added Successfully");
                    response.sendRedirect("../add-book-category");

                }
            } else {
                request.setAttribute("error", "Category already exists");
                response.sendRedirect("../add-book-category");
            }
        }
    }

URL web.xml 中的模式是:/process/*

public class adminControllerServlet extends HttpServlet {

    AdminDataAccess ada = new AdminDataAccess();    
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            try (PrintWriter out = response.getWriter()) {
                String path = request.getPathInfo();
    
                // out.print(path);
                switch (path) {
    
                    ...
                    case "/insert_category":
                        InsertCategory(request, response);
                        break;
                }
    
            }
        }

下面是数据库查询:

   // Insert Queries
    public int insert_category(Category category) {
        int inserted = 0;
        try {
            Connection connect = con.getConnection();
            String query = "INSERT INTO Category (category_name) values(?)";
            PreparedStatement ps = connect.prepareStatement(query);
            ps.setString(1, category.getCategory_name());

            inserted = ps.executeUpdate();
        } catch (SQLException ex) {
             Logger.getLogger(AdminDataAccess.class.getName()).log(Level.SEVERE, null, ex);
        }
        return inserted;
    }

我现在遇到的问题是,当我 运行 项目在输入字段中输入正确的参数后将新类别插入数据库的类别列表时,我最终遇到了这个异常:

Severe:   com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'category_name' cannot be null
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1040)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359).....

请问我还能做什么,因为我相信 request.getParameter("categoryname") 应该在表单输入字段中获取数据

我解决了问题。

问题是我的代码在模型包中没有构造函数 Category(categoryname)

这使得

不可能
Category category = new Category(categoryname);

上班。

所以,这只是意味着我一开始使用 getCategoryname() 时没有得到任何 categoryname,这本来是要传递给数据库插入语句,导致异常。