I am tring to create a CRUD car web application, but I am getting an error. The error is in my servlet class for the web appliation. Whenever I try to use the insert, update or delete Car methods, I am told "The method is undefined for the type CarDao". I have posted my classes for my application down below. The error specifically occurs at the response.sendRedirect methods in the CarServlet class. Does anyone know how to resolve this issue please?
package com.xadmin.carmanagement.bean;
/**
* Car.java
* This is a model class represents a Car entity
*
*/
public class Car {
protected int id;
protected String name;
protected String model;
protected String type;
public Car() {
}
public Car(String name, String model, String type) {
super();
this.name = name;
this.model = model;
this.type = type;
}
public Car(int id, String name, String model, String type) {
super();
this.id = id;
this.name = name;
this.model = model;
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
package com.xadmin.carmanagement.dao;
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.List;
import com.xadmin.carmanagement.bean.Car;
/**
* AbstractDAO.java This DAO class provides CRUD database operations for the
* table users in the database.
*
*/
public class CarDao {
private String jdbcURL = "jdbc:mysql://localhost:3306/userdb?useSSL=false";
private String jdbcUsername = "root";
private String jdbcPassword = "rootpasswordgiven";
private static final String INSERT_CARS_SQL = "INSERT INTO cars" + " (name, model, type) VALUES "
+ " (?, ?, ?);";
private static final String SELECT_CARS_BY_ID = "select id,name,model, from cars where id =?";
private static final String SELECT_ALL_CARS = "select * from cars";
private static final String DELETE_CARS_SQL = "delete from cars where id = ?;";
private static final String UPDATE_CARS_SQL = "update car set name = ?,model= ?, type =? where id = ?;";
public CarDao() {
}
protected Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
public void insertCar(Car car) throws SQLException {
System.out.println(INSERT_CARS_SQL);
// try-with-resource statement will auto close the connection.
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_CARS_SQL)) {
preparedStatement.setString(1, car.getName());
preparedStatement.setString(2, car.getModel());
preparedStatement.setString(3, car.getType());
System.out.println(preparedStatement);
preparedStatement.executeUpdate();
} catch (SQLException e) {
printSQLException(e);
}
}
public Car selectCar(int id) {
Car car = null;
// Step 1: Establishing a Connection
try (Connection connection = getConnection();
// Step 2:Create a statement using connection object
PreparedStatement preparedStatement = connection.prepareStatement(SELECT_CARS_BY_ID);) {
preparedStatement.setInt(1, id);
System.out.println(preparedStatement);
// Step 3: Execute the query or update query
ResultSet rs = preparedStatement.executeQuery();
// Step 4: Process the ResultSet object.
while (rs.next()) {
String name = rs.getString("name");
String model = rs.getString("model");
String type = rs.getString("type");
car = new Car(id, name, model, type);
}
} catch (SQLException e) {
printSQLException(e);
}
return car;
}
public List<Car> selectAllCars() {
// using try-with-resources to avoid closing resources (boiler plate code)
List<Car> cars = new ArrayList<>();
// Step 1: Establishing a Connection
try (Connection connection = getConnection();
// Step 2:Create a statement using connection object
PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_CARS);) {
System.out.println(preparedStatement);
// Step 3: Execute the query or update query
ResultSet rs = preparedStatement.executeQuery();
// Step 4: Process the ResultSet object.
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String model = rs.getString("model");
String type = rs.getString("type");
cars.add(new Car(id, name, model, type));
}
} catch (SQLException e) {
printSQLException(e);
}
return cars;
}
public boolean deleteCar(int id) throws SQLException {
boolean rowDeleted;
try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement(DELETE_CARS_SQL);) {
statement.setInt(1, id);
rowDeleted = statement.executeUpdate() > 0;
}
return rowDeleted;
}
public boolean updateCar(Car car) throws SQLException {
boolean rowUpdated;
try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement(UPDATE_CARS_SQL);) {
System.out.println("updated Car:"+statement);
statement.setString(1, car.getName());
statement.setString(2, car.getModel());
statement.setString(3, car.getType());
statement.setInt(4, car.getId());
rowUpdated = statement.executeUpdate() > 0;
}
return rowUpdated;
}
private void printSQLException(SQLException ex) {
for (Throwable e : ex) {
if (e instanceof SQLException) {
e.printStackTrace(System.err);
System.err.println("SQLState: " + ((SQLException) e).getSQLState());
System.err.println("Error Code: " + ((SQLException) e).getErrorCode());
System.err.println("Message: " + e.getMessage());
Throwable t = ex.getCause();
while (t != null) {
System.out.println("Cause: " + t);
t = t.getCause();
}
}
}
}
}
package com.xadmin.carmanagement.web;
import java.io.IOException;
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 com.xadmin.carmanagement.dao.CarDao;
import com.xadmin.carmanagement.bean.Car;
/**
* ControllerServlet.java
* This servlet acts as a page controller for the application, handling all
* requests from the user.
*/
@WebServlet("/")
public class CarServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private CarDao carDao;
public void init() {
carDao = new CarDao();
}
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":
insertCar(request, response);
break;
case "/delete":
deleteCar(request, response);
break;
case "/edit":
showEditForm(request, response);
break;
case "/update":
updateCar(request, response);
break;
default:
listCar(request, response);
break;
}
} catch (SQLException ex) {
throw new ServletException(ex);
}
}
private void listCar(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException, ServletException {
List<Car> listCar = carDao.selectAllCars();
request.setAttribute("listCar", listCar);
RequestDispatcher dispatcher = request.getRequestDispatcher("car-list.jsp");
dispatcher.forward(request, response);
}
private void showNewForm(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("car-form.jsp");
dispatcher.forward(request, response);
}
private void showEditForm(HttpServletRequest request, HttpServletResponse response)
throws SQLException, ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Car existingCar = carDao.selectCar(id);
RequestDispatcher dispatcher = request.getRequestDispatcher("car-form.jsp");
request.setAttribute("car", existingCar);
dispatcher.forward(request, response);
}
private void insertCar(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
String name = request.getParameter("name");
String model = request.getParameter("model");
String type = request.getParameter("type");
Car newCar = new Car(name, model, type);
carDao.insertCar(newCar);
response.sendRedirect("list");
}
private void updateCar(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String model = request.getParameter("model");
String type = request.getParameter("type");
Car book = new Car(id, name, model, type);
carDao.updateCar(book);
response.sendRedirect("list");
}
private void deleteCar(HttpServletRequest request, HttpServletResponse response)
throws SQLException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
carDao.deleteCar(id);
response.sendRedirect("list");
}
}
Aucun commentaire:
Enregistrer un commentaire