首先,我是 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");
}
}
}
web.xml 中的 URL 模式是:/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")應該已經在表單輸入欄位中獲取資料
uj5u.com熱心網友回復:
我解決了這個問題。
問題是我的代碼Category(categoryname)在模型包中沒有建構式 。
這使得它不可能
Category category = new Category(categoryname);
上班。
所以,這只是意味著我一開始沒有得到任何 用于傳遞給資料庫插入陳述句的categorynameusing getCategoryname(),從而導致例外。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/318176.html
上一篇:不登錄就無法訪問靜態資源目錄(我使用的是springsecurity)
下一篇:傳遞函式引數(元組內的字典)
