Servlet Help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Servlet Help

  1. #1
    Join Date
    Feb 2005
    Posts
    55

    Servlet Help

    I want to create a Registration Servlet allowing a user to type in their details and save it to a mySQL database. Below are all the classes i have used. the Registration Servelt is throwing an SQlException shown below.
    } catch (SQLException se) {
    out.println("<p>There has been a problem creating your account. Please contact the site administrator via email to <a href=\"mailto:admin@sfikjfdk.com\">this address</a></p>");
    out.println("<p>SQL Error executing query='"+query+"'");
    }

    My question is how can I fix it to make my servlet work, ive tried everything i could think of.

    /*
    * RegistrationServlet.java
    *
    * Created on 2 September 2005, 20:04
    */

    import java.io.*;
    import java.net.*;
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.SQLException;
    import javax.servlet.*;
    import javax.servlet.http.*;

    /**
    *
    * @author Trixxma
    * @version
    */
    public class RegistrationServlet extends HttpServlet {

    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet ReservationServlet</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Account Creation</h1>");

    //Get parameters for new user record
    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");
    String email = request.getParameter("email");
    String username = request.getParameter("uname");
    String password = request.getParameter("password");
    String query = "INSERT INTO Users VALUES ('"+username+"','"+password+"','"+firstname+"','"+lastname+"','"+email+"')";
    // write the user to the selected database
    try {
    Connection conn = DataManager.getInstance().getConnection();
    Statement stmt = conn.createStatement();
    stmt.execute(query);
    User newUser = new User(username, firstname, lastname, email);
    // add the user to the session and redirect to the menu
    request.getSession().setAttribute("user", newUser);
    // response.sendRedirect("tipping");
    } catch (SQLException se) {
    out.println("<p>There has been a problem creating your account. Please contact the site administrator via email to <a href=\"mailto:admin@sfikjfdk.com\">this address</a></p>");
    out.println("<p>SQL Error executing query='"+query+"'");
    }
    out.println("</body>");
    out.println("</html>");
    out.close();
    }

    // <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
    */
    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
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    processRequest(request, response);
    }

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

    /*
    * User.java
    *
    * Created on 2 September 2005, 20:33
    *
    * To change this template, choose Tools | Options and locate the template under
    * the Source Creation and Management node. Right-click the template and choose
    * Open. You can then make changes to the template in the Source Editor.
    */

    /**
    *
    * @author Trixxma
    */
    public class User {
    private String userName, firstName, lastName, email;

    /** Creates a new instance of User */
    public User(String userName, String firstName, String lastName, String email) {
    this.setFirstName(firstName);
    this.setLastName(lastName);
    this.setUserName(userName);
    this.setEmail(email);
    }

    public String getUserName() {
    return userName;
    }

    public void setUserName(String userName) {
    this.userName = userName;
    }

    public String getFirstName() {
    return firstName;
    }

    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }

    public String getLastName() {
    return lastName;
    }

    public void setLastName(String lastName) {
    this.lastName = lastName;
    }

    public void setEmail(String email){
    this.email = email;
    }

    public String getEmail(){
    return email;
    }
    }

    /*
    * DataManager.java
    *
    * Created on 28 August 2005, 18:30
    *
    * To change this template, choose Tools | Options and locate the template under
    * the Source Creation and Management node. Right-click the template and choose
    * Open. You can then make changes to the template in the Source Editor.
    */

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ResourceBundle;

    public class DataManager {
    ResourceBundle rb;

    private DataManager() {
    rb = ResourceBundle.getBundle("database");
    this.loadDriver();
    }

    public void loadDriver() {
    try {
    new com.mysql.jdbc.Driver();
    Class.forName(rb.getString("jdbc.driver")).newInstance();
    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }

    public Connection getConnection() throws SQLException {
    return DriverManager.getConnection(
    rb.getString("jdbc.url"),
    rb.getString("jdbc.username"),
    rb.getString("jdbc.password"));
    }

    public static DataManager getInstance() {
    return new DataManager();
    }

    // method to test connection
    public static void main(String args[]) {
    try {
    DataManager dm = new DataManager();
    dm.getConnection();
    System.out.println("DataManager is working correctly");
    } catch (SQLException sqle) {
    System.out.println(sqle.getMessage());
    }
    }

    }

    CREATE DATABASE movie;
    USE movie;
    CREATE TABLE Users (
    uname VARCHAR(15) NOT NULL UNIQUE,
    password VARCHAR(15) NOT NULL,
    admin BOOLEAN,
    firstname VARCHAR(20),
    lastname VARCHAR(30) );

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    You don't show what the error was or what line it occured on!
    What is the message in the SQLException se object?
    And what was the query text?

    Copy and paste here the FULL text of the error message that is displayed.

  3. #3
    Join Date
    Feb 2005
    Posts
    55
    In the RegistrationServlet there is the following code

    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");
    String email = request.getParameter("email");
    String username = request.getParameter("uname");
    String password = request.getParameter("password");
    String query = "INSERT INTO Users VALUES ('"+username+"','"+password+"','"+firstname+"','"+lastname+"','"+email+"')";

    try {
    Connection conn = DataManager.getInstance().getConnection();
    Statement stmt = conn.createStatement();
    stmt.execute(query);
    User newUser = new User(username, firstname, lastname, email);
    // add the user to the session and redirect to the menu
    request.getSession().setAttribute("user", newUser);
    // response.sendRedirect("tipping");
    } catch (SQLException se) {
    out.println("<p>There has been a problem creating your account. Please contact the site administrator via email to <a href=\"mailto:admin@sfikjfdk.com\">this address</a></p>");
    out.println("<p>SQL Error executing query='"+query+"'");
    }

    currently I am only getting what is printed in the catch block which is
    There has been a problem creating your account. Please contact the site administrator via email this address
    SQL Error executing query="trixma","123","Bob","brown","apple@gmail.com")'

    So my query is throwing an SQLException and i want to stop this. The query is meant to store the results into mySQL and currently it is not but throwing an exception, I want help in fixing my code so it no longers throws the exception but stores the data into the database Users.

  4. #4
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    It would help if you would show exactly what the SQLException was and what line it was on.
    You can do that by adding a line of code within the catch block:
    ex.printStackTrace();
    There has been a problem
    doesn't help much.
    Perhaps once you see what the error is you can correct it.

    Also it would be better if you copy and paste the EXACT contents of the error message and not type it in manually.
    Last edited by Norm; 09-04-2005 at 10:43 AM.

  5. #5
    Join Date
    Feb 2005
    Posts
    55
    I did copy it, the message is exactly how i saw it in html view, i ran getMessage() and found that I needed to fix up some file permissions, and once i did this i fixed the problem. Thanks for your help Norm, its most appriciated

Similar Threads

  1. Replies: 2
    Last Post: 02-05-2006, 02:04 AM
  2. typical problem, pls help
    By SATISH in forum Java
    Replies: 1
    Last Post: 09-29-2000, 09:33 AM
  3. Replies: 1
    Last Post: 08-10-2000, 12:35 PM
  4. Replies: 1
    Last Post: 08-10-2000, 12:33 PM
  5. Replies: 0
    Last Post: 04-04-2000, 04:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center