1 Replies - 3862 Views - Last Post: 23 March 2015 - 05:26 AM

#1 ricososwavay   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 113
  • Joined: 11-February 14

How to insert data into database in java web application

Posted 20 March 2015 - 11:33 AM

I am using a connection pool and prepared statement to insert my values into the database using this method here:

public static int insert(Checkout checkout) {
        ConnectionPool pool = ConnectionPool.getInstance();
        Connection connection = pool.getConnection();
        PreparedStatement ps = null;

        String query
                = "INSERT INTO Checkout (FirstName, LastName, EmailAddress, "
                + "BookTitle, DueDate) "
                + "VALUES (?, ?, ?, ?, ?)";
        try {
            ps = connection.prepareStatement(query);
           
            ps.setString(1, checkout.getFirstName());
            ps.setString(2, checkout.getLastName());
            ps.setString(3, checkout.getEmailAddress());
            ps.setString(4, checkout.getBookTitle());
            ps.setString(5, checkout.getFormattedDate());
            return ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e);
            return 0;
        } finally {
            DBUtil.closePreparedStatement(ps);
            pool.freeConnection(connection);
        }
    }


I want to be able to access this method in my servlet. So in my servlet I have a doCheckout that gets all the request parameters from the form. I am drawing on blank on how to get the data from the form inserted into the database and add the Checkout object to the request object so that the Thank You page can display it.
 private String doCheckout(HttpServletRequest request,
            HttpServletResponse response) {
        
        
        String firstName= request.getParameter("first_name");
        String lastName= request.getParameter("last_name");
        String emailAddress= request.getParameter("email_address");
        String bookTitle= request.getParameter("book_title");
        Checkout checkout=new Checkout();
        String date= checkout.getFormattedDate();
        
        
        Checkout check =new Checkout(firstName,lastName,emailAddress,bookTitle,date);
        CheckoutDb.insert(check);
        return "/thankyou.jsp";
    }


Is This A Good Question/Topic? 0
  • +

Replies To: How to insert data into database in java web application

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 984
  • View blog
  • Posts: 3,879
  • Joined: 13-June 14

Re: How to insert data into database in java web application

Posted 23 March 2015 - 05:26 AM

Your insert() method looks like it's updating the database. Is something not working with it? One thing I notice is that that method is supposed to return an int, but you only have a return statement in your catch block. What about the other paths through there?

Regarding adding the Checkout instance to the request object, you just need to call setAttribute() on the request object. There are some examples here. Also, remember to consult the API docs for HttpServletRequest.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1