| package com.servlet;
|
|
|
| import java.io.IOException;
|
| import java.sql.Connection;
|
| import java.sql.DriverManager;
|
| import java.sql.PreparedStatement;
|
| import java.sql.ResultSet;
|
| import java.sql.SQLException;
|
|
|
| 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.entity.Customer;
|
|
|
| @WebServlet("/fetchCustomer")
|
| public class fetchCustomer extends HttpServlet {
|
| Connection conn = null;
|
| PreparedStatement stmt = null;
|
| ResultSet rs = null;
|
| private static final String url = "jdbc:derby:C:\\Users\\samar\\BankDB;create=true";
|
|
|
| protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
| String custId = request.getParameter("customerId");
|
| System.out.println("Customer Id: " + custId);
|
| System.out.println("Inside the servlet.");
|
|
|
| try {
|
| Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
|
| conn = DriverManager.getConnection(url);
|
| stmt = conn.prepareStatement("SELECT * FROM Customers WHERE id = ?");
|
|
|
| stmt.setString(1, custId);
|
| rs = stmt.executeQuery();
|
|
|
| if (rs.next()) {
|
| System.out.println("Customer found.");
|
| // Create a Customer object with database values
|
| Customer customer = new Customer(
|
| rs.getString("id"),
|
| rs.getString("name"),
|
| rs.getString("acc_num"),
|
| rs.getString("ifsc"),
|
| rs.getInt("acc_bal"),
|
| rs.getString("email"),
|
| rs.getString("contact_num"),
|
| rs.getString("address"),
|
| rs.getDate("dob").toLocalDate(),
|
| rs.getString("gender"),
|
| rs.getString("marital_status"),
|
| rs.getString("aadhar"),
|
| rs.getString("pan")
|
| );
|
|
|
| // Set the customer object in the request
|
| request.setAttribute("customer", customer);
|
|
|
| System.out.println(customer.toString());
|
| } else {
|
| // If no customer found, set an error message
|
| request.setAttribute("errorMessage", "Customer ID not found. Please try again.");
|
| }
|
|
|
| // Forward to the JSP page
|
| RequestDispatcher dispatcher = request.getRequestDispatcher("Transactions.jsp");
|
| dispatcher.forward(request, response);
|
|
|
| } catch (SQLException e) {
|
| throw new ServletException("Database error", e);
|
| } catch (ClassNotFoundException e) {
|
| // TODO Auto-generated catch block
|
| e.printStackTrace();
|
| }finally {
|
| try {
|
| if (rs != null) rs.close();
|
| if(stmt != null) stmt.close();
|
| if(conn != null) conn.close();
|
| }catch(SQLException e){
|
| e.printStackTrace();
|
| }
|
| }
|
| }
|
| }
|