First of all I'm new to JSP and trying out some projects
Please I have a jsp form that would be submited to a database here is the form code:
...<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>...
for the category name I have to get the name=categoryname
to submit as a parameter to at the back end
Here is the servlet code:
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 PATTERN IN web.xml was:/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;
}
}
}
Below is the database query:
// 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;
}
The problem I have now is when I run the project to insert a new category into the list of categories in the database after inputting the right parameters into the input field I end up with this exception:
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).....
please what else can I do because I believe request.getParameter("categoryname")
ought to have gotten the data in the form input field
CodePudding user response:
I solve the issue.
The problem was my code did not have a constructor Category(categoryname)
in the model package.
That made it impossible for
Category category = new Category(categoryname);
to work.
So, it simply means I was not getting any categoryname
at first using getCategoryname()
which was meant to be passed to the database insert statement, Causing the exception.