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.
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.
Then begin with an interface for persistence:
Then implement that DAO interface:
A utility class like this might help you. Use it to see if you can successfully connect to your database and perform some operations: