Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7185699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:28:21+00:00 2026-05-28T18:28:21+00:00

I’m reading the book, Murach’s Java Servlets and JSP 2nd Edition. He provide a

  • 0

I’m reading the book, Murach’s Java Servlets and JSP 2nd Edition.
He provide a database that is automatically installed through a .bat file.

I tried his examples and it is working fine.

Now I’m trying to create my on app using his database and nothing is happening. Here is the following code:

JSP:

<%-- 
    Document   : index
    Created on : 27/01/2012, 9:20:02 AM
    Author     : Camus
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Form</title>
    </head>
    <body>
        <h1>Type your information here!</h1>
        <form action="addToEmailList" method="get">
            <input type="text" name="firstName">First Name<br>
            <input type="text" name="lastName"> Last Name<br>
            <input type="text" name="emailAddress"> email
            <input type="submit" value="Submit">
        </form>
    </body>
</html>

Servlet

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;


/**
 *
 * @author Camus
 */
public class addToEmailList extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

            String firstName = request.getParameter("firstName");
            String lastName = request.getParameter("lastName");
            String emailAddress = request.getParameter("emailAddress");

            User user = new User();
            user.setFirstName(firstName);
            user.setLastName(lastName);
            user.setEmailAddress(emailAddress);

            String query ="DELETE FROM user WHERE FirstName = 'Diogo'";

            try {
                String dbURL="jdbc:mysql://localhost:3306/murach";
            String username ="root";
            String password = "sesame";
                    Connection connection = DriverManager.getConnection(dbURL, username, password);

                    Statement statement = connection.createStatement();
                    statement.executeUpdate(query);
                connection.close();
            } 
            catch (Exception e) {
                e.printStackTrace();
        }


             String URL = "/result.jsp";
             RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(URL);
             dispatcher.forward(request, response);


    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

I’ve already tried many things and when I check on the database nothing is happening. I’ve tried different queries and again nothing is updated. Do you guys have a clue what I’m doing wrong?

Please help me. I reckon it should be a basic mistake but as I’m learning and really do not know what is going on.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:28:22+00:00Added an answer on May 28, 2026 at 6:28 pm

    I wonder why you’re using a DELETE query when you submit a form? I would think that should be an INSERT.

    I think you’re got too much going on in one problem. You’re learning about JSPs, servlets, and databases all at once. There’s too much happening.

    Computer science is all about decomposition: solve complex problems by breaking them up into smaller, more manageable ones.

    You don’t need a servlet or a JSP to get the database going. Get that working first.

    Here’s what I’d recommend: start with a Person class.

    package model;
    
    public class Person {
        private String firstName;
        private String lastName;
        private String email;
    
    // Add constructors, getters, equals, hashcode, etc.
    }
    

    Then begin with an interface for persistence:

    package persistence;
    
    public interface PersonDao {
        List<Person> find(String lastName, String firstName);
        List<Person> find();
        void save(Person p);
        void update(Person p);
        void delete(Person p);
    }
    

    Then implement that DAO interface:

    package persistence;
    
    public class PersonDaoImpl implements PersonDao {
        private Connection connection;
    
        public PersonDaoImpl(Connection connection) {
            this.connection = connection;
        }
    
        // Implement all the JDBC methods here.
    }
    

    A utility class like this might help you. Use it to see if you can successfully connect to your database and perform some operations:

    package persistence;
    
    import java.sql.*;
    import java.util.*;
    
    /**
     * util.DatabaseUtils
     * User: Michael
     * Date: Aug 17, 2010
     * Time: 7:58:02 PM
     */
    public class DatabaseUtils
    {
        private static final String DEFAULT_DRIVER = "oracle.jdbc.driver.OracleDriver";
        private static final String DEFAULT_URL = "jdbc:oracle:thin:@host:1521:database";
        private static final String DEFAULT_USERNAME = "username";
        private static final String DEFAULT_PASSWORD = "password";
    /*
        private static final String DEFAULT_DRIVER = "org.postgresql.Driver";
        private static final String DEFAULT_URL = "jdbc:postgresql://localhost:5432/party";
        private static final String DEFAULT_USERNAME = "pgsuper";
        private static final String DEFAULT_PASSWORD = "pgsuper";
    */
    /*
        private static final String DEFAULT_DRIVER = "com.mysql.jdbc.Driver";
        private static final String DEFAULT_URL = "jdbc:mysql://localhost:3306/party";
        private static final String DEFAULT_USERNAME = "party";
        private static final String DEFAULT_PASSWORD = "party";
    */
    
        public static void main(String[] args)
        {
            long begTime = System.currentTimeMillis();
    
            String driver = ((args.length > 0) ? args[0] : DEFAULT_DRIVER);
            String url = ((args.length > 1) ? args[1] : DEFAULT_URL);
            String username = ((args.length > 2) ? args[2] : DEFAULT_USERNAME);
            String password = ((args.length > 3) ? args[3] : DEFAULT_PASSWORD);
    
            Connection connection = null;
    
            try
            {
                connection = createConnection(driver, url, username, password);
                DatabaseMetaData meta = connection.getMetaData();
                System.out.println(meta.getDatabaseProductName());
                System.out.println(meta.getDatabaseProductVersion());
    
                String sqlQuery = "SELECT PERSON_ID, FIRST_NAME, LAST_NAME FROM PERSON ORDER BY LAST_NAME";
                System.out.println("before insert: " + query(connection, sqlQuery, Collections.EMPTY_LIST));
    
                connection.setAutoCommit(false);
                String sqlUpdate = "INSERT INTO PERSON(FIRST_NAME, LAST_NAME) VALUES(?,?)";
                List parameters = Arrays.asList( "Foo", "Bar" );
                int numRowsUpdated = update(connection, sqlUpdate, parameters);
                connection.commit();
    
                System.out.println("# rows inserted: " + numRowsUpdated);
                System.out.println("after insert: " + query(connection, sqlQuery, Collections.EMPTY_LIST));
            }
            catch (Exception e)
            {
                rollback(connection);
                e.printStackTrace();
            }
            finally
            {
                close(connection);
                long endTime = System.currentTimeMillis();
                System.out.println("wall time: " + (endTime - begTime) + " ms");
            }
        }
    
        public static Connection createConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException
        {
            Class.forName(driver);
    
            if ((username == null) || (password == null) || (username.trim().length() == 0) || (password.trim().length() == 0))
            {
                return DriverManager.getConnection(url);
            }
            else
            {
                return DriverManager.getConnection(url, username, password);
            }
        }
    
        public static void close(Connection connection)
        {
            try
            {
                if (connection != null)
                {
                    connection.close();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    
    
        public static void close(Statement st)
        {
            try
            {
                if (st != null)
                {
                    st.close();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    
        public static void close(ResultSet rs)
        {
            try
            {
                if (rs != null)
                {
                    rs.close();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    
        public static void rollback(Connection connection)
        {
            try
            {
                if (connection != null)
                {
                    connection.rollback();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    
        public static List<Map<String, Object>> map(ResultSet rs) throws SQLException
        {
            List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
    
            try
            {
                if (rs != null)
                {
                    ResultSetMetaData meta = rs.getMetaData();
                    int numColumns = meta.getColumnCount();
                    while (rs.next())
                    {
                        Map<String, Object> row = new HashMap<String, Object>();
                        for (int i = 1; i <= numColumns; ++i)
                        {
                            String name = meta.getColumnName(i);
                            Object value = rs.getObject(i);
                            row.put(name, value);
                        }
                        results.add(row);
                    }
                }
            }
            finally
            {
                close(rs);
            }
    
            return results;
        }
    
        public static List<Map<String, Object>> query(Connection connection, String sql, List<Object> parameters) throws SQLException
        {
            List<Map<String, Object>> results = null;
    
            PreparedStatement ps = null;
            ResultSet rs = null;
    
            try
            {
                ps = connection.prepareStatement(sql);
    
                int i = 0;
                for (Object parameter : parameters)
                {
                    ps.setObject(++i, parameter);
                }
    
                rs = ps.executeQuery();
                results = map(rs);
            }
            finally
            {
                close(rs);
                close(ps);
            }
    
            return results;
        }
    
        public static int update(Connection connection, String sql, List<Object> parameters) throws SQLException
        {
            int numRowsUpdated = 0;
    
            PreparedStatement ps = null;
    
            try
            {
                ps = connection.prepareStatement(sql);
    
                int i = 0;
                for (Object parameter : parameters)
                {
                    ps.setObject(++i, parameter);
                }
    
                numRowsUpdated = ps.executeUpdate();
            }
            finally
            {
                close(ps);
            }
    
            return numRowsUpdated;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.