0

Here is the poblem
Class file I am not sure if it reading the id and saving it
Java file (Error: After end of result set) when trying to display SQL, (Error: in the id)

package homework1;

import java.text.FieldPosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.Connection;

public class RegisterEntry {

    //Registration
    private String firstName;
    private String lastName;
    private String email1;
    private String areaCode;
    private String prefix;
    private String suffix;
    private String password1;
//  //Time
//  private Date timestamp;
//  //New Post
//  private String npTitle;
//  private String npPost;
//  //Comment Post
//  private String cpTitle;
//  private String cpComment;

    public RegisterEntry( int i, String string2, String string3, String string4, String string5, String string6, String string,String string7) {
    }

    public RegisterEntry(String firstName, String lastName , String email1, String areaCode, String prefix, String suffix, String password1) {
        //Registration
        this.firstName = firstName;
        this.lastName = lastName;
        this.email1 = email1;
        this.areaCode = areaCode;
        this.prefix = prefix;
        this.suffix = suffix;
        this.password1 = password1;
    }
    //1.Registration Start
//  public int getUid() {
//      return uid;
//  }
//  public void setUid(int uid) {
//      this.uid = uid;
//  }
    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 String getEmail1() {
        return email1;
    }

    public void setEmail1(String email1) {
        this.email1 = email1;
    }

    public String getAreaCode() {
        return areaCode;
    }

    public void setAreaCode(String areaCode) {
        this.areaCode = areaCode;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public String getPassword1() {
        return password1;
    }

    public void setPassword1(String password1) {
        this.password1 = password1;
    }
    //1.End
        //--------------------------------------------//
//  //2.Time Start
//  public Date getTimestamp() {
//      return timestamp;
//  }
//  //2.End
//      //--------------------------------------------//
//  //3.New Post Start
//  public String getNpTitle() {
//      return npTitle;
//  }
//  public void setNpTitle(String npTitle) {
//      this.npTitle = npTitle;
//  }
//  public String getNpPost() {
//      return npPost;
//  }
//  public void setNpPost(String npPost) {
//      this.npPost = npPost;
//  }
//  //3.End
//  //--------------------------------------------//
//  //4.Comment Post Start
//  public String getCpTitle() {
//      return cpTitle;
//  }
//  public void setCpTitle(String cpTitle) {
//      this.cpTitle = cpTitle;
//  }
//  public String getCpComment() {
//      return cpComment;
//  }
//  public void setCpComment(String cpComment) {
//      this.cpComment = cpComment;
//  }
//  //4.End

//  public String getFormattedTimeStamp() {
//      SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd hh:mmaa" );
//      StringBuffer sb = new StringBuffer();
//      sdf.format( timestamp, sb, new FieldPosition( 0 ) );
//      
//      return sb.toString();
//  }

//  public String toString() {
//      return "Posts"+cpTitle+""+npPost+""+cpTitle+""+cpComment;
//  }

}




package homework1;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
//import java.INSERT_TABLE.ResultSet;
import java.sql.SQLException;
//import java.INSERT_TABLE.Statement;
//import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Arrays;

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 javax.servlet.http.HttpSession;
//
//import com.mysql.jdbc.Driver;

import homework1.RegisterEntry;

@WebServlet("/homework1/Register")
public class Register extends HttpServlet {
    private static final long serialVersionUID = 1L;
    // SQL Users
    // private static final String CREATE_TABLE
    // ="CREATE TABLE IF NOT EXISTS `Users` ("+
    // " `id` INT( 11 ) NOT NULL AUTO_INCREMENT ,"+
    // " `firstName` VARCHAR( 100 ) NOT NULL ,"+
    // " `lastName` VARCHAR( 100 ) NOT NULL ,"+
    // " `email1` VARCHAR( 100 ) NOT NULL ,"+
    // " `areaCode` VARCHAR( 100 ) NOT NULL ,"+
    // " `prefix` VARCHAR( 100 ) NOT NULL ,"+
    // " `suffix` VARCHAR( 100 ) NOT NULL ,"+
    // " `password1` VARCHAR( 100 ) NOT NULL ,"+
    // " PRIMARY KEY ( `id` ) "+
    // " ) TYPE = MYISAM ";
    private static final String INSERT_TABLE = "INSERT INTO Users (id,firstName,lastName,email1,areaCode,prefix,suffix,password1) values (?,?,?,?,?,?,?,?)";
    private Connection connection = null;

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        String username = "cs320stu26";
        String password = "!vEcncKO";
        String host = "cs3.calstatela.edu";
        String port = "3306";
        String dbName = "cs320stu26";
        String connectionURL = "jdbc:mysql://" + host + ":" + port + "/"
                + dbName;

        boolean error = request.getAttribute("error") != null;
        boolean[] formValidate = null;
        if (error)
            formValidate = (boolean[]) request.getAttribute("formValidate");

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try {

            Class.forName("org.gjt.mm.mysql.Driver");
            connection = DriverManager.getConnection(connectionURL, username,
                    password);

            // HEADER HTML START
            out.println("<!DOCTYPE html>");
            out.println("<html lang=\"en\">");
            out.println("<head>");
            out.println("<meta charset=\"utf-8\">");
            out.println("<title>CS320 Message Board</title>");
            out.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
            out.println("<meta name=\"description\" content=\"\">");
            out.println("<meta name=\"author\" content=\"\">");

            out.println("<!-- Le styles -->");
            out.println("<!--rel=\"alternate stylesheet\"-->");
            out.println("<link href=\"" + request.getContextPath()
                    + "/css/bootstrap.css\" rel=\"stylesheet\">");
            out.println("<style>");
            out.println("  body {");
            out.println("   padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */");
            out.println(" }");
            out.println("</style>");
            out.println("<link href=\"" + request.getContextPath()
                    + "/css/bootstrap-responsive.css\" rel=\"stylesheet\">");

            out.println("<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->");
            out.println("<!--[if lt IE 9]>");
            out.println("<script src=\"http://html5shim.googlecode.com/svn/trunk/html5.js\"></script>");
            out.println("<![endif]-->");

            out.println("<!-- Le fav and touch icons -->");
            out.println("<link rel=\"shortcut icon\" href=\"ico/favicon.ico\">");
            out.println("<!--<link rel=\"apple-touch-icon-precomposed\" sizes=\"144x144\" href=\""
                    + request.getContextPath()
                    + "/ico/apple-touch-icon-144-precomposed.png\">");
            out.println("<link rel=\"apple-touch-icon-precomposed\" sizes=\"114x114\" href=\""
                    + request.getContextPath()
                    + "/ico/apple-touch-icon-114-precomposed.png\">");
            out.println("<link rel=\"apple-touch-icon-precomposed\" sizes=\"72x72\" href=\""
                    + request.getContextPath()
                    + "/ico/apple-touch-icon-72-precomposed.png\">");
            out.println("<link rel=\"apple-touch-icon-precomposed\" href=\""
                    + request.getContextPath()
                    + "/ico/apple-touch-icon-57-precomposed.png\">-->");
            out.println("</head>");

            out.println("<body>");

            out.println("<div class=\"navbar navbar-fixed-top\">");
            out.println("<div class=\"navbar-inner\">");
            out.println("<div class=\"container\">");
            out.println("<a class=\"btn btn-navbar\" data-toggle=\"collapse\" data-target=\".nav-collapse\">");
            out.println("<span class=\"icon-bar\"></span>");
            out.println("<span class=\"icon-bar\"></span>");
            out.println("<span class=\"icon-bar\"></span>");
            out.println("</a>");
            out.println("<a class=\"brand\" href=\"#\">CS320 Message Board</a>");
            out.println("<div class=\"nav-collapse\">");
            out.println("<ul class=\"nav\">");
            out.println("<li><a href=\"../homework1/Home\">Home</a></li>");
            out.println("<li><a href=\"../homework1/Posts\">Posts</a></li>");
            out.println("<li><a href=\"#\">Admin</a></li>");
            // out.println("<li><a href=\"../homework2/posts\">Posts</a></li>");
            // out.println("<li><a href=\"../homework2/admin\">Admin</a></li>");
            out.println("</ul>");

            out.println("</div><!--/.nav-collapse -->");
            out.println("</div>");
            out.println("</div>");
            out.println("</div>");
            // HEADER HTML END

            // CONTAINER HTML START
            out.println("<div class=\"container\">");

            out.println("<div class=\"row\">");
            out.println("<div class=\"offset3 span6\">");

            out.println("<form class=\"form-horizontal\" action=\"../homework1/Register\" method=\"post\">");
            out.println("<fieldset>");
            out.println("<legend>New User Registration</legend>");
            out.println("<div class=\"control-group"
                    + ((error && !formValidate[0]) ? " error" : "") + "\">");
            out.println("<label class=\"control-label\" for=\"firstName\">First Name</label>");
            out.println("<div class=\"controls\">");
            out.println("<input type=\"text\" class=\"input-xlarge\" id=\"firstName\" name=\"firstName\" value=\""
                    + (request.getParameter("firstName") == null ? "" : request
                            .getParameter("firstName")) + "\"/>");

            out.println("<p class=\"help-block\">Must contain only alphabets.  Spaces are not allowed.</p>");

            out.println("</div>");
            out.println("</div>");

            out.println("<div class=\"control-group"
                    + ((error && !formValidate[1]) ? " error" : "") + "\">");
            out.println("<label class=\"control-label\" for=\"lastName\">Last Name</label>");
            out.println("<div class=\"controls\">");
            out.println("<input type=\"text\" class=\"input-xlarge\" id=\"lastName\" name=\"lastName\" value=\""
                    + (request.getParameter("lastName") == null ? "" : request
                            .getParameter("lastName")) + "\"/>");

            out.println("<p class=\"help-block\">Must contain only alphabets.  Spaces are not allowed.</p>");
            out.println("</div>");
            out.println("</div>");

            out.println("<div class=\"control-group"
                    + ((error && !formValidate[2]) ? " error" : "") + "\">");
            out.println("<label class=\"control-label\" for=\"email1\">E-mail Address</label>");
            out.println("<div class=\"controls\">");
            out.println("<input type=\"text\" class=\"input-xlarge\" id=\"email1\" name=\"email1\" value=\""
                    + (request.getParameter("email1") == null ? "" : request
                            .getParameter("email1")) + "\"/>");

            out.println("<p class=\"help-block\">Must be a valid e-mail address.</p>");
            out.println("</div>");
            out.println("</div>");

            out.println("<div class=\"control-group"
                    + ((error && !formValidate[2]) ? " error" : "") + "\">");
            out.println("<label class=\"control-label\" for=\"email2\">Re-Enter E-mail Address</label>");
            out.println("<div class=\"controls\">");
            out.println("<input type=\"text\" class=\"input-xlarge\" id=\"email2\" name=\"email2\" value=\""
                    + (request.getParameter("email2") == null ? "" : request
                            .getParameter("email2")) + "\"/>");

            out.println("<p class=\"help-block\">Must be a valid e-mail address.</p>");
            out.println("</div>");
            out.println("</div>");

            out.println("<div class=\"control-group"
                    + ((error && !formValidate[3]) ? " error" : "") + "\">");
            out.println("<label class=\"control-label\" for=\"areaCode\">Telephone #</label>");
            out.println("<div class=\"controls\">");
            out.println("( <input type=\"text\" class=\"input-small\" id=\"areaCode\" name=\"areaCode\" value=\""
                    + (request.getParameter("areaCode") == null ? "" : request
                            .getParameter("areaCode")) + "\"/> )");
            out.println("<input type=\"text\" class=\"input-small\" id=\"prefix\" name=\"prefix\" value=\""
                    + (request.getParameter("prefix") == null ? "" : request
                            .getParameter("prefix")) + "\"/> - ");
            out.println("<input type=\"text\" class=\"input-small\" id=\"suffix\" name=\"suffix\" value=\""
                    + (request.getParameter("suffix") == null ? "" : request
                            .getParameter("suffix")) + "\"/>");

            out.println("<p class=\"help-block\">Must only contain numbers.</p>");
            out.println("</div>");
            out.println("</div>");

            out.println("<div class=\"control-group"
                    + ((error && !formValidate[4]) ? " error" : "") + "\">");
            out.println("<label class=\"control-label\" for=\"password1\">Password</label>");
            out.println("<div class=\"controls\">");
            out.println("<input type=\"password\" class=\"input-xlarge\" id=\"password1\" name=\"password1\" />");

            out.println("<p class=\"help-block\">Your account password. Must contain at least one letter and number.</p>");
            out.println("</div>");
            out.println("</div>");

            out.println("<div class=\"control-group"
                    + ((error && !formValidate[4]) ? " error" : "") + "\">");
            out.println("<label class=\"control-label\" for=\"password2\">Re-Enter Password</label>");
            out.println("<div class=\"controls\">");
            out.println("<input type=\"password\" class=\"input-xlarge\" id=\"password2\" name=\"password2\" />");

            out.println("<p class=\"help-block\">Your account password. Must contain at least one letter and number.</p>");
            out.println("</div>");
            out.println("</div>");

            out.println("<div class=\"control-group\">");
            out.println("<label class=\"control-label\"></label>");
            out.println("<div class=\"controls\">");
            out.println("<hr />");
            out.println("<button type=\"submit\"  name='submit' value='Register' class=\"btn btn-primary\">Register</button>");
            out.println("</div>");
            out.println("</div>");

            out.println("</fieldset>");
            out.println("</form>");

            out.println("</div>");
            out.println("</div>");

            out.println("</div><!-- /container -->");
            // CONTAINER HTML END

            // FOOTER HTML START
            out.println("<!-- Le javascript");
            out.println("================================================== -->");
            out.println("<!-- Placed at the end of the document so the pages load faster -->");
            out.println("<script src=\"" + request.getContextPath()
                    + "/js/jquery-1.7.2.min.js\"></script>");
            out.println("<script src=\"" + request.getContextPath()
                    + "/js/bootstrap.min.js\"></script>");

            out.println("</body>");
            out.println("</html>");
            // FOOTER HTML END
        } catch (ClassNotFoundException e) {
            out.println("Couldn't load database driver: " + e.getMessage());
        } catch (SQLException e) {
            out.println("SQLException caught: " + e.getMessage());
        } catch (Exception e) {
            out.println(e);
        } finally {
            // Always close the database connection.
            try {
                if (connection != null)
                    connection.close();

            } catch (SQLException ignored) {
                out.println(ignored);
            }
        }
    }

    @SuppressWarnings("unused")
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        String username = "cs320stu26";
        String password = "!vEcncKO";
        String host = "cs3.calstatela.edu";
        String port = "3306";
        String dbName = "cs320stu26";
        String connectionURL = "jdbc:mysql://" + host + ":" + port + "/"
                + dbName;

        String id = "";
        id = request.getParameter("id");

        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String email1 = request.getParameter("email1");
        String email2 = request.getParameter("email2");
        String areaCode = request.getParameter("areaCode");
        String prefix = request.getParameter("prefix");
        String suffix = request.getParameter("suffix");
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");

        ResultSet rs;

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        boolean[] formValidate = new boolean[5];
        Arrays.fill(formValidate, true);

        try {

            Class.forName("org.gjt.mm.mysql.Driver");
            connection = DriverManager.getConnection(connectionURL, username,
                    password);

            // Form Validation
            if (!firstName.matches("[a-zA-Z]+")) {
                formValidate[0] = false;
                // Must contain only alphabets. Spaces are not allowed.
            }
            if (!lastName.matches("[a-zA-Z]+")) {
                formValidate[1] = false;
                // Must be a valid e-mail address
            }
            if (!email1
                    .matches("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")) {
                formValidate[2] = false;
                // Must be a valid e-mail address
            }
            if (!email2.equals(email1)) {
                formValidate[2] = false;
                // Must only contain numbers.
            }
            if (!areaCode.matches("\\d+") || !prefix.matches("\\d+")
                    || !suffix.matches("\\d+")) {
                formValidate[3] = false;
                // Your account password. Must contain at least one letter and
                // number.
            }
            if (!password1.matches("((?=.*\\d)(?=.*[a-zA-Z]).+)")) {
                formValidate[4] = false;
                // Your account password. Must contain at least one letter and
                // number.
            }
            if (!password2.equals(password1)) {
                formValidate[4] = false;
            }

            request.setAttribute("formValidate", formValidate);

            for (boolean b : formValidate)
                if (!b) {
                    request.setAttribute("error", true);
                    doGet(request, response);
                    return;
                }

            PreparedStatement pst1 = connection.prepareStatement(INSERT_TABLE);
            pst1.setString(1, id);
            pst1.setString(2, firstName);
            pst1.setString(3, lastName);
            pst1.setString(4, email1);
            pst1.setString(5, areaCode);
            pst1.setString(6, prefix);
            pst1.setString(7, suffix);
            pst1.setString(8, password1);
            System.out.println("passed setting");

            int rowUpdate = pst1.executeUpdate();
            // show that the new account has been created
            System.out.println(" Hello : " + firstName + "");

            ArrayList<RegisterEntry> users = new ArrayList<RegisterEntry>();

            Statement statement = connection.createStatement();
            String query = "SELECT id, firstName, lastName, email1, areaCode, prefix, suffix, password1 FROM Users";
            ResultSet resultSet = statement.executeQuery(query);  

            while(resultSet.next() )
                users.add(new RegisterEntry(resultSet.getInt("id"),
                                   resultSet.getString("firstName"),
                                   resultSet.getString("lastName"),
                                   resultSet.getString("email1"),
                                   resultSet.getString("areaCode"),
                                   resultSet.getString("prefix"),
                                   resultSet.getString("suffix"),
                                   resultSet.getString("password1")));
            request.setAttribute("Users", users);

            System.out.print(resultSet.getString("firstName") + "::");
            System.out.print(resultSet.getString("lastName") + "::");
            System.out.print(resultSet.getInt("id") + "::");
            System.out.print(resultSet.getInt("email1") + "::");
            System.out.print(resultSet.getString("password1") + "::");



//          response.sendRedirect("Posts");
            pst1.close();
        } catch (ClassNotFoundException e) {
            out.println("Couldn't load database driver: " + e.getMessage());
        } catch (SQLException e) {
            out.println("SQLException caught: " + e.getMessage());
        } catch (Exception e) {
            out.println(e);
        } finally {
            // Always close the database connection.
            try {
                if (connection != null)
                    connection.close();
            } catch (SQLException ignored) {
                out.println(ignored);
            }
        }
    }

}
2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by DavidKroukamp
0

Please give the full error message thats printed from the console.. with the line number and thrown exception etc and of course the section of code the error is pointing too

Edited by DavidKroukamp

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.