Click here to Skip to main content
15,904,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've tried to update my MySQL table through the JSP servlet but it's not updating on the table. Also, there are no errors showing !!

What I have tried:

Java
package fuel.web;

import java.io.IOException;

import java.sql.Date;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fuel.dao.FuelDAO;
import fuel.model.Fuel;

/**
 * ControllerServlet.java
 * This servlet acts as a page controller for the application, handling all
 * requests from the user.
 * @email Ramesh Fadatare
 */

@WebServlet("/")
public class FuelServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private FuelDAO fuelDAO;
	
	public void init() {
		fuelDAO = new FuelDAO();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getServletPath();

		try {
			switch (action) {
			case "/new":
				showNewForm(request, response);
				break;
			case "/insert":
				insertUser(request, response);
				break;
			case "/delete":
				deleteUser(request, response);
				break;
			case "/edit":
				showEditForm(request, response);
				break;
			case "/update":
				updateUser(request, response);
				break;
			default:
				listFuel(request, response);
				break;
			}
		} catch (SQLException ex) {
			throw new ServletException(ex);
		}
	}

	private void listFuel(HttpServletRequest request, HttpServletResponse response)
			throws SQLException, IOException, ServletException {
		List<fuel> listFuel = fuelDAO.selectAllUsers();
		request.setAttribute("listUser", listFuel);
		RequestDispatcher dispatcher = request.getRequestDispatcher("fuel-list.jsp");
		dispatcher.forward(request, response);
	}

	private void showNewForm(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		RequestDispatcher dispatcher = request.getRequestDispatcher("fuel-form.jsp");
		dispatcher.forward(request, response);
	}

	private void showEditForm(HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		Fuel existingUser = fuelDAO.selectUser(id);
		RequestDispatcher dispatcher = request.getRequestDispatcher("fuel-form.jsp");
		request.setAttribute("fuel", existingUser);
		dispatcher.forward(request, response);

	}

	private void insertUser(HttpServletRequest request, HttpServletResponse response) 
			throws SQLException, IOException {
		String GasSID = request.getParameter("GasSID");
		String FuelType= request.getParameter("FuelType");
		float CStock = Float .parseFloat(request.getParameter("CStock"));
		float RefAmount = Float .parseFloat(request.getParameter("RefAmount"));
		Date date = Date.valueOf(request.getParameter("Date"));
		Fuel newFuel = new Fuel(GasSID, FuelType, CStock, RefAmount, date);
		fuelDAO.insertUser(newFuel);
		response.sendRedirect("list");
	}

	private void updateUser(HttpServletRequest request, HttpServletResponse response) 
			throws SQLException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String GasSID = request.getParameter("GasSID");
		String FuelType= request.getParameter("FuelType");
		float CStock = Float .parseFloat(request.getParameter("CStock"));
		float RefAmount = Float .parseFloat(request.getParameter("RefAmount"));
		Date date = Date.valueOf(request.getParameter("Date"));

		Fuel book = new Fuel(id, GasSID, FuelType, CStock, RefAmount, date);
		fuelDAO.updateUser(book);
		response.sendRedirect("list");
	}

	private void deleteUser(HttpServletRequest request, HttpServletResponse response) 
			throws SQLException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		fuelDAO.deleteUser(id);
		response.sendRedirect("list");

	}

}





HTML
////////////// from-list.jsp/////////////////////////////////////


<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<title>User Management Application



	
		
			<div>
				<a href="https://www.javaguides.net" class="navbar-brand"> User
					Management App </a>
			</div>

			<ul class="navbar-nav">
				<li><a href="<%=request.getContextPath()%>/list" class="nav-link">Fuel</a></li>			</ul>
		
	
	<br>

	<div class="row">
		

		<div class="container">
			<h3 class="text-center">List of Users</h3>
			<hr>
			<div class="container text-left">

				<a href="<%=request.getContextPath()%>/new" class="btn btn-success">Add
					New User</a>
			</div>
			<br>
			
									
					<c:foreach var="user" items="${listUser}">

						
					
				<table class="table table-bordered"><thead>					<tr>						<th>ID</th>						<th>Name</th>						<th>Email</th>						<th>Country</th>						<th>Country2</th>						<th>Country3</th>						<th>Actions</th>					</tr>				</thead>				<tbody><tr>							<td><c:out value="${fuel.id}"></td>							<td><c:out value="${fuel.GasSID}"></td>							<td><c:out value="${fuel.FuelType}"></td>							<td><c:out value="${fuel.CStock}"></td>							<td><c:out value="${fuel.RefAmount}"></td>								<td><c:out value="${fuel.date}"></td>							<td><a>Edit</a>
								     <a
 href="delete?id=<c:out value='${fuel.id}' />">Delete</td>						</tr>					</tbody>			</table>
		</div>
	</div>
Posted
Updated 24-Sep-22 23:13pm
v3
Comments
Richard MacCutchan 22-Sep-22 5:14am    
Have you checked the server console to see if there are any error messages?
Member 15226818 23-Sep-22 14:08pm    
yea there is no any errors are showing

1 solution

Start by using the debugger to find out exactly what is in the three parameters you apply to the UNPDATE query: dt, tot, and fname.
At a guess, fname is the wrong value, since the query references the FuelType column and unless fname matches exactly at least one column value you will get no rows updated, and no error.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900