Am pretty new to programming in general and I have encountered a certain problem in my Java web app where my product information is not updating despite the adding and deleting of a product from the MySQL database working. When I hit submit on the update page the program would simply return me a blank page and the values would not update in the MySQL database.
Netbeans log and Apache tomcat logs are not returning error so I can only think that the redirection is going to a blank non-existing page instead but I have tried adding "../" to the form action but it did not work.
HTML form code:
<form method="post" action="product.update">
<div class="row gtr-uniform">
<!-- hidden form -->
<input type="hidden" name="id" value="${product.getID()}">
<div class="col-6 col-12-xsmall">
<input type="text" id="name" placeholder="Product name" value="${product.getName()}" required />
</div>
<div class="col-6 col-12-xsmall">
<input type="text" id="quantity" placeholder="Available quantity" value="${product.getProductQuantity()}" required />
</div>
<div class="col-6 col-12-xsmall">
<input type="text" id="price" placeholder="Product price" value="${product.getPrice()}" required />
</div>
<!-- Break -->
<div class="col-12">
<ul class="actions">
<li><input type="submit" value="Submit Form" class="primary" /></li>
</ul>
</div>
</div>
</form>
Update product Servlet:
response.setContentType("text/html;charset=UTF-8");
String str_id=request.getParameter("id");
String name=request.getParameter("name");
String str_price=request.getParameter("price");
String str_quantity=request.getParameter("quantity");
ArrayList<String> errormsg=new ArrayList<>();
boolean success=true;
if(str_id==null || name==null || str_price==null || str_quantity==null){
errormsg.add("Error: Null parameters.");
success=false;
}
else if(str_id.isEmpty() || name.isEmpty() || str_price.isEmpty() || str_quantity.isEmpty()){
errormsg.add("Error: Empty parameters.");
success=false;
}
else{
try{
int id=Integer.parseInt(str_id);
ProductDAO func=new ProductDAO();
ProductBean bean=func.getProductByID(id);
Double price=Double.parseDouble(str_price);
int quantity=Integer.parseInt(str_quantity);
success=func.updatePrice(bean, price) && func.updateName(bean, name) && func.updateStock(bean, quantity);
}
catch(NumberFormatException e){
success=false;
errormsg.add("Error: IDs are not integers.");
}
RequestDispatcher rd=request.getRequestDispatcher("WEB-INF/product/productupdate_success.jsp");
if(success==false){
rd=request.getRequestDispatcher("WEB-INF/errors/error.jsp");
request.setAttribute("error", errormsg);
}
rd.forward(request, response);
}
Product Bean class:
public class ProductBean implements java.io.Serializable{
private int id;
private Double price;
private String name;
private int quantity;
public ProductBean(){
this.id=-1;
this.price=0.0;
this.name="empty_noitem";
this.quantity=0;
}
public ProductBean(int id, double prc, String nm, int qty){
this.id=id;
this.price=prc;
this.name=nm;
this.quantity=qty;
}
// setters
public void updateID(int id){
this.id=id;
}
public void updatePrice(double prc){
this.price=prc;
}
public void updateName(String nm){
this.name=nm;
}
public void updateProductQuantity(int qty){
this.quantity=qty;
}
// getters
public int getID(){
return this.id;
}
public double getPrice(){
return this.price;
}
public String getName(){
return this.name;
}
public int getProductQuantity(){
return this.quantity;
}
}
Product DAO class:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ResourceBundle;
/**
*
* @author SpicyAsh
*/
public class ProductDAO implements java.io.Serializable{
private String driver;
private String url;
private String user;
private String pass;
private boolean loadClass(){
try{
Class.forName(driver);
}catch (ClassNotFoundException e){
e.printStackTrace();
return false;
}
return true;
}
public void loadSQL(){
ResourceBundle sql=ResourceBundle.getBundle("system.sql.sql");
this.url=sql.getString("url");
this.user=sql.getString("user");
this.pass=sql.getString("password");
this.driver=sql.getString("driver");
}
public ProductDAO(String driver, String url, String user, String pass)
{
this.url=url;
this.user=user;
this.pass=pass;
this.driver=driver;
}
public ProductDAO()
{
loadSQL();
}
public boolean exists(ProductBean bean){
if(getProductByID(bean.getID())!=null || getProductByName(bean.getName())!=null){
return true;
}
// else
return false;
}
public boolean addProduct(ProductBean bean){
loadClass();
String sql="insert into product(product_name, price, quantity) values(?,?,?)";
boolean success=true;
// writing try-catch like this automatically closes the resources
// this is called try-with-resources
try(Connection conn=DriverManager.getConnection(url,user,pass);
PreparedStatement ps=conn.prepareStatement(sql);){
conn.setAutoCommit(false);
ps.setString(1, bean.getName());
ps.setDouble(2, bean.getPrice());
ps.setInt(3, bean.getProductQuantity());
ps.executeUpdate();
conn.commit();
}
catch(SQLException e){
success=false;
}
return success;
}
public ProductBean getProductByID(int inid){
loadClass();
String sql="select * from product where ID=?";
ProductBean prod=new ProductBean();
try(Connection conn=DriverManager.getConnection(url, user, pass);
PreparedStatement ps=conn.prepareStatement(sql);){
ps.setInt(1, inid);
ResultSet rs=ps.executeQuery();
while(rs.next()){
int id=rs.getInt("ID");
String name=rs.getString("product_name");
double price=rs.getDouble("price");
int quantity=rs.getInt("quantity");
prod.updateID(id);
prod.updateName(name);
prod.updatePrice(price);
prod.updateProductQuantity(quantity);
}
}
catch(SQLException e){
e.printStackTrace();
}
return prod;
}
public ProductBean getProductByName(String inname){
loadClass();
String sql="select * from product where product_name=?";
ProductBean prod=new ProductBean();
try(Connection conn=DriverManager.getConnection(url, user, pass);
PreparedStatement ps=conn.prepareStatement(sql);){
ps.setString(1, inname);
ResultSet rs=ps.executeQuery();
while(rs.next()){
int id=rs.getInt("ID");
String name=rs.getString("product_name");
double price=rs.getDouble("price");
int quantity=rs.getInt("quantity");
prod.updateID(id);
prod.updateName(name);
prod.updatePrice(price);
prod.updateProductQuantity(quantity);
}
}
catch(SQLException e){
e.printStackTrace();
}
return prod;
}
public ArrayList<ProductBean> getAllProducts(){
loadClass();
String sql="select * from product";
ProductBean bean=new ProductBean();
ArrayList <ProductBean> arrList=new ArrayList<ProductBean>();
try(Connection conn=DriverManager.getConnection(url, user, pass);
PreparedStatement ps=conn.prepareStatement(sql);){
ResultSet rs=ps.executeQuery();
while(rs.next()){
bean=new ProductBean();
int id=rs.getInt("ID");
String name=rs.getString("product_name");
double price=rs.getDouble("price");
int quantity=rs.getInt("quantity");
bean.updateID(id);
bean.updateName(name);
bean.updatePrice(price);
bean.updateProductQuantity(quantity);
arrList.add(bean);
}
}
catch(SQLException e){
e.printStackTrace();
}
return arrList;
}
// update methods
public boolean updatePrice(ProductBean row, Double newPrice){
if(!exists(row)){
return false;
}
loadClass();
boolean success=true;
String sql="update product set price=? where id=?";
try(Connection conn=DriverManager.getConnection(url,user,pass);
PreparedStatement ps=conn.prepareStatement(sql);){
conn.setAutoCommit(false);
ps.setDouble(1, newPrice);
ps.setInt(2, row.getID());
ps.executeUpdate();
conn.commit();
}
catch(SQLException e){
success=false;
e.printStackTrace();
}
return success;
}
// update methods
public boolean updateName(ProductBean row, String newName){
if(!exists(row)){
return false;
}
loadClass();
boolean success=true;
String sql="update product set product_name=? where id=?";
try(Connection conn=DriverManager.getConnection(url,user,pass);
PreparedStatement ps=conn.prepareStatement(sql);){
conn.setAutoCommit(false);
ps.setString(1, newName);
ps.setInt(2, row.getID());
ps.executeUpdate();
conn.commit();
}
catch(SQLException e){
success=false;
e.printStackTrace();
}
return success;
}
public boolean updateStock(ProductBean row, int newStock){
if(!exists(row)){
return false;
}
loadClass();
boolean success=true;
String sql="update product set quantity=? where id=?";
try(Connection conn=DriverManager.getConnection(url,user,pass);
PreparedStatement ps=conn.prepareStatement(sql);){
conn.setAutoCommit(false);
ps.setInt(1, newStock);
ps.setInt(2, row.getID());
ps.executeUpdate();
conn.commit();
}
catch(SQLException e){
success=false;
e.printStackTrace();
}
return success;
}
// delete method (ONLY ADMIN/OWNDER)
public boolean removeProduct(ProductBean row){
if(!exists(row)){
return false;
}
loadClass();
boolean success=true;
String sql="delete from product where id=?";
try(Connection conn=DriverManager.getConnection(url, user, pass);
PreparedStatement ps=conn.prepareStatement(sql);){
conn.setAutoCommit(false);
ps.setInt(1,row.getID());
ps.executeUpdate();
conn.commit();
}
catch(SQLException e){
success=false;
e.printStackTrace();
}
return success;
}
}
Product table structure in MySQL:
| Field | Type | Null | Key | Default | Extra |
|---|---|---|---|---|---|
| id | int | NO | PRI | NULL | auto_increment |
| product_name | varchar(50) | YES | NULL | ||
| price | double | YES | NULL | ||
| quantity | int | YES | 0 |
Aucun commentaire:
Enregistrer un commentaire