JSP form doesn't send to database. Using beans

When i submit data from my form it doesnt save to the database... no error occurs...

i can retrieve from the database but not save to it...

heres the code:

test.jsp

 <%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.util.*" %>
<%@ page import="my.beans.StudentBean"%>

<jsp:useBean id="studentData" scope="request"
             class="my.beans.StudentDataBean"/>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Words View</title>
        <style type="text/css">
               table, tr, td, th
               {
                    text-algn: center;
                    font-size: .9em;
                    border: 3px groove;
                    padding: 3px;
                    background-color: #eee9e9;
               }
        </style>
    </head>
    <body>
        <h1>Student List</h1>
        <table border="1">
            <tr>
                <th>
                    <h4>First Name</h4>
                </th>
                <th>
                    <h4>Last Name</h4>
                </th>
                <th>
                    <h4>Comment</h4>
                </th>
                <th>
                    <h4>Email</h4>
                </th>
                </tr>
                    <%
                        ArrayList<StudentBean> studentList = studentData.getStudentList();
                        Iterator studentListIterator = studentList.iterator();
                        StudentBean student;

                        while (studentListIterator.hasNext()){
                            student = (StudentBean) studentListIterator.next();
                    %>
                <tr>
                    <td><%= student.getFirstName()%></td>
                    <td><%= student.getLastName()%></td>
                    <td><%= student.getComment()%></td>
                    <td><%= student.getEmail()%></td>
                </tr>
                <% }
                %>
        </table>
    </body>
</html>

formTest.jsp

<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<jsp:useBean id = "student" scope = "page"
             class = "my.beans.StudentBean" />
<jsp:useBean id = "studentD" scope = "page"
             class = "my.beans.StudentDataBean" />

<html>
    <form method="post" action="test.jsp">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Student Login</title>
        </head>
        <body>
            <jsp:setProperty name = "student" property = "*" />
            <% // start scriptlet
                if (student.getFirstName() == null
                        || student.getLastName() == null
                        || student.getEmail() == null
                        || student.getComment() == null) {
            %>
            Enter forename, surname, student ID and email address to <br />
            register.<br />
            <br />
            <table border="1">
                <tr>
                    <td>First Name:</td>
                    <td>
                        <input type="text" name="first" />
                    </td>
                </tr>
                <tr>
                    <td>Last Name:</td>
                    <td>
                        <input type="text" name="last" />
                    </td>
                </tr>
                <tr>
                    <td>Email:</td>
                    <td>
                        <input type="text" name="email" />
                    </td>
                </tr>
                <tr>
                    <td>Comment:</td>
                    <td>
                        <input type="text" name="comment" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="submit" value="Submit" />
                    </td>
                </tr>
            </table>
        </body>
    </form>
    <%            } else {
        studentD.addStudent(student);
    %>
    <jsp:forward page="test.jsp" />
    <%
        }
    %>
</html>

And then my beans...

StudentBean.java

package my.beans;


public class StudentBean {

        /***********\
         * Globals *
        \***********/

    private String firstName;
    private String lastName;
    private String comment;
    private String email;

        public StudentBean(){

        }

        // get/set for First Name
    public void setFirstName(String f) {
        firstName = f;
    }
        public String getFirstName(){
                return firstName;
        }

        // get/set for Last Name
    public void setLastName(String l) {
        lastName = l;
    }
        public String getLastName(){
                return lastName;
        }

        // get/set for comment
    public void setComment(String co) {
        comment = co;
    }
        public String getComment(){
                return comment;
        }

        // get/set for Email
    public void setEmail(String em) {
        email = em;
    }
        public String getEmail(){
                return email;
        }
}

StudentDataBean.java

package my.beans;



import java.sql.SQLException;
import javax.sql.rowset.CachedRowSet;
import java.util.ArrayList;
import com.sun.rowset.CachedRowSetImpl; // CachedRowSet implementation

public class StudentDataBean {

   private CachedRowSet rowSet;

   // construct TitlesBean object 
   public StudentDataBean() throws ClassNotFoundException, SQLException{
      // load the MySQL driver
      Class.forName("com.mysql.jdbc.Driver");

      // specify properties of CachedRowSet
      rowSet = new CachedRowSetImpl();
      rowSet.setUrl("jdbc:mysql://localhost:3306/test"); 
      rowSet.setUsername("root");
      rowSet.setPassword("");

      // obtain list of titles
      rowSet.setCommand("SELECT firstName, lastName, email, comment FROM guests" );
      rowSet.execute();
   } // end StudentDataBean constructor

   // return an ArrayList of StudnetBeans
   public ArrayList<StudentBean> getStudentList() throws SQLException{
      ArrayList<StudentBean> studentList = new ArrayList<StudentBean>();

      rowSet.beforeFirst(); // move cursor before the first row

      // get row data
      while (rowSet.next()){
         StudentBean student = new StudentBean();

         student.setFirstName(rowSet.getString(1));
         student.setLastName(rowSet.getString(2));
         student.setEmail(rowSet.getString(3));
         student.setComment(rowSet.getString(4));

         studentList.add( student ); 
      } // end while

      return studentList;
   } // end method getStudentList

   // insert a Student in student database
   public void addStudent(StudentBean student) throws SQLException
   {
      rowSet.moveToInsertRow(); // move cursor to the insert row

      // update the three columns of the insert row 
      rowSet.updateString( 1, student.getFirstName() ); 
      rowSet.updateString( 2, student.getLastName() ); 
      rowSet.updateString( 3, student.getEmail() ); 
      rowSet.updateString( 4, student.getComment() ); 
      rowSet.insertRow(); // insert row to rowSet
      rowSet.moveToCurrentRow(); // move cursor to the current row
              try{
            rowSet.acceptChanges();
        }
        catch(Exception e){
            System.out.println("Exception caught at line 67: " + e);
        } 
   } 
}

Answers


Problem solved: it was an error with my java conventions not matching.


Need Your Help

Android: How to import contact from phone?

java android android-sqlite contacts

I am making a contact application. I wish to import all the contacts stored in the phone and sim cards into my application, but I don't know how to write the code. Any help and hints or links to us...

Mock httpcontext.current.request.files

c# httpcontext nunit-2.5.9

I am implementing nUnit Test case for one of my method called, UploadFile(), some thing like below