Click here to Skip to main content
15,907,183 members
Home / Discussions / Java
   

Java

 
AnswerRe: Download JDK 1.6_04 Pin
toxcct20-Oct-08 21:24
toxcct20-Oct-08 21:24 
Questioncolumns side by side Pin
ic3e18-Oct-08 14:01
ic3e18-Oct-08 14:01 
AnswerRe: columns side by side Pin
sunil_jvfrm1-Nov-08 1:30
sunil_jvfrm1-Nov-08 1:30 
Questionwondering why it doesnt create another text field Pin
srinath158317-Oct-08 23:11
srinath158317-Oct-08 23:11 
Questionsun.net.smtp.SmtpProtocolException help Pin
davilism16-Oct-08 7:12
davilism16-Oct-08 7:12 
QuestionScanner Not Found Pin
Kevin Marois15-Oct-08 12:17
professionalKevin Marois15-Oct-08 12:17 
AnswerRe: Scanner Not Found Pin
toxcct15-Oct-08 23:16
toxcct15-Oct-08 23:16 
QuestionSQL exception Pin
An Enigma14-Oct-08 16:49
An Enigma14-Oct-08 16:49 
Hi,

First time programming JSP as I have to do it for an uni assignment. Basically I have created a register HTML and JSP page:

<br />
<!-- Register.html --><br />
<html><br />
<head><br />
   <title>Register Form</title><br />
</head><br />
<br />
<body><br />
   <h1>Hotel Account</h1><br />
   <form><br />
	<br />
<br />
      ACTION="Register"  METHOD="POST"><br />
<br />
      <pre><br />
      Customer Name:             <input type="text" name="CustomerName"><br />
      Customer No:               <input type="text" name="CustomerNo"><br />
      Customer Type:             <input type="text" name="CustomerType"><br />
      Customer Comp:             <input type="text" name="CustomerComp"><br />
      Customer Address:          <input type="text" name="CustomerAddress"><br />
      Nationality:  		 <input type="text" name="Nationality"><br />
      Passport No:   		 <input type="text" name="PassportNo"><br />
      Travel Agency:         	 <input type="text" name="TravelAgency"><br />
      Password:                  <input type="text" name="Password"><br />
      </input></input></input></input></input></input></input></input></input></pre><br />
     <br />
      <input type="SUBMIT" value="Submit"><br />
   </input></form><br />
</body><br />
</html><br />


From there what I am trying to do is insert all the data gathered from the inputs into an DB.

Now I have decided to do 4 Java classes and 1 servlet inorder to achieve this.

I have created the account class to use the getter and setter methods:

<br />
/*<br />
 * Account.java<br />
 *<br />
 * Created on October 14, 2008, 9:45 PM<br />
 *<br />
 * To change this template, choose Tools | Template Manager<br />
 * and open the template in the editor.<br />
 */<br />
<br />
/**<br />
 *<br />
 * <br />
 */<br />
public abstract class Account<br />
{<br />
  String CustomerName, CustomerType, CustomerComp, CustomerAddress, Nationality, PassportNo, TravelAgency, Password;<br />
  int CustomerNo;<br />
  <br />
       <br />
       /**<br />
   * default constructor<br />
   */<br />
  public Account()<br />
  {<br />
    CustomerName = "";<br />
    CustomerNo = 0;<br />
    CustomerType = "";<br />
    CustomerComp = "";<br />
    CustomerAddress = "";<br />
    Nationality = "";<br />
    PassportNo = "";<br />
    TravelAgency = "";<br />
    Password = "";<br />
    <br />
  }<br />
  /**<br />
   * Constructor with parameters<br />
   * @param AccNo int<br />
   * @param cName String<br />
   * @param cAddress String<br />
   * @param bal String<br />
   * @param sDate Date<br />
   */<br />
   <br />
  public Account(String cName, int cNo, String cType, String cComp, String cAdd, String n, String pNo, String tAgency, String pass)<br />
                 <br />
  {<br />
    CustomerName = cName;<br />
    CustomerNo = cNo;<br />
    CustomerType = cType;<br />
    CustomerComp = cComp;<br />
    CustomerAddress = cAdd;<br />
    Nationality = n;<br />
    PassportNo = pNo;<br />
    TravelAgency = tAgency;<br />
    Password = pass;<br />
   }<br />
  /**<br />
   * Set Account Number<br />
   * @param AccNo int<br />
   */<br />
  public void setCustomerName(String cName)<br />
  {<br />
    CustomerName = cName;<br />
  }<br />
  /**<br />
   * Set Customer Name<br />
   * @param cName String<br />
   */<br />
  public void setCustomerNo(int cNo)<br />
  {<br />
    CustomerNo = cNo;<br />
  }<br />
  /**<br />
   * Set Customer Address<br />
   * @param cAddress String<br />
   */<br />
  public void setCustomerType(String cType)<br />
  {<br />
    CustomerType = cType;<br />
  }<br />
  /**<br />
   * Set Balance<br />
   * @param bal String<br />
   */<br />
  public void setCustomerComp(String cComp)<br />
  {<br />
    CustomerComp = cComp;<br />
  }<br />
  /**<br />
   * Set Start Date<br />
   * @param sDate Date<br />
   */<br />
  public void setCustomerAddress(String cAdd)<br />
  {<br />
    CustomerAddress = cAdd;<br />
  }<br />
<br />
  public void setNationality(String n)<br />
  {<br />
    Nationality = n;<br />
  }<br />
  <br />
  public void setPassportNo(String pNo)<br />
  {<br />
    PassportNo = pNo;<br />
  }<br />
  <br />
   public void setTravelAgency(String tAgency)<br />
  {<br />
    TravelAgency = tAgency;<br />
  }<br />
   <br />
   public void setPassword(String pass)<br />
  {<br />
    Password = pass;<br />
  }<br />
  <br />
  public void setAllAccountData(String cName, int cNo, String cType, String cComp, String cAdd, String n, String pNo, String tAgency, String pass)<br />
  {<br />
    CustomerName = cName;<br />
    CustomerNo = cNo;<br />
    CustomerType = cType;<br />
    CustomerComp = cComp;<br />
    CustomerAddress = cAdd;<br />
    Nationality = n;<br />
    PassportNo = pNo;<br />
    TravelAgency = tAgency;<br />
    Password = pass;<br />
  }<br />
  /**<br />
   * Get Account Number<br />
   * @return int<br />
   */<br />
  public String getCustomerName()<br />
  {<br />
    return CustomerName;<br />
  }<br />
  /**<br />
   * Get Customer Name<br />
   * @return String<br />
   */<br />
  public int getCustomerNo()<br />
  {<br />
    return CustomerNo;<br />
  }<br />
  /**<br />
   * Get Customer Address<br />
   * @return String<br />
   */<br />
  public String getCustomerType()<br />
  {<br />
    return CustomerType;<br />
  }<br />
  /**<br />
   * Get Balance<br />
   * @return double<br />
   */<br />
  public String getCustomerComp()<br />
  {<br />
    return CustomerComp;<br />
  }<br />
  /**<br />
   * Get Start Date<br />
   * @return Date<br />
   */<br />
  public String getCustomerAddress()<br />
  {<br />
    return CustomerAddress;<br />
  }<br />
<br />
  public String getNationality()<br />
  {<br />
    return Nationality;<br />
  }<br />
<br />
  public String getPassportNo()<br />
  {<br />
    return PassportNo;<br />
  }<br />
<br />
  public String getTravelAgency()<br />
  {<br />
    return TravelAgency;<br />
  }<br />
<br />
  public String getPassword()<br />
  {<br />
    return Password;<br />
  }<br />
  <br />
  /**<br />
   * get all account data<br />
   * @return String<br />
   */<br />
  public String getAllAccountData()<br />
  {<br />
   return "CustomerName = " + CustomerName +<br />
        "; CustomerNo = " + CustomerNo +<br />
        "; CustomerType = " + CustomerType +<br />
        "; CustomerComp = " + CustomerComp +<br />
        "; CustomerAddress = " + CustomerAddress +<br />
        "; Nationality = " + Nationality +<br />
        "; PassportNo = " + PassportNo +<br />
        "; TravelAgency = " + TravelAgency +<br />
        "; Password = " + Password;<br />
  }<br />


Then I made an Account DB class to hold customer info:
<br />
/*<br />
 * AccountDB.java<br />
 *<br />
 * Created on October 14, 2008, 10:06 PM<br />
 *<br />
 * To change this template, choose Tools | Template Manager<br />
 * and open the template in the editor.<br />
 */<br />
<br />
/**<br />
 *<br />
 *<br />
 */<br />
import java.sql.*;<br />
/**<br />
 * <p>Title: workshop07</p><br />
 * <p>Description: Database class</p><br />
 * <p>Copyright: Copyright (c) 2005</p><br />
 * <p>Company:ECU </p><br />
 * @author Leisa Armstrong<br />
 * @version 1.0<br />
 */<br />
<br />
<br />
/**<br />
 * Class to create connection to a database and<br />
 * and demonstrate basic SQL queries<br />
 */<br />
public class AccountDB<br />
{<br />
   /**<br />
    * the connection object<br />
    */<br />
   static Connection connection;<br />
   // select odbc driver<br />
   static final String dbDriver =<br />
      "sun.jdbc.odbc.JdbcOdbcDriver";<br />
   static final String dbUrl = "jdbc:odbc:HotelDatabase1";<br />
   // use the BankAccount database<br />
   static final String dbName = "HotelDatabase1";<br />
   // set dbUser to any user on your Windows OS<br />
   static final String dbUser = " ";<br />
   // set dbPassword to Windows password for dbUser<br />
   static final String dbPassword = " ";<br />
<br />
   /**<br />
    * method to open the connection<br />
    */<br />
   public Connection getConnection()<br />
   {<br />
     System.out.println("Getting Database driver" );<br />
     try<br />
     {<br />
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver" );<br />
<br />
     }<br />
     catch( ClassNotFoundException e )<br />
     {<br />
       System.err.println(<br />
       "Cannot load database driver: for DB2, "<br />
       + "your classpath must include "<br />
       + "SQLLIB\\JAVA12\\DB2JAVA.ZIP." );<br />
      }<br />
<br />
<br />
<br />
      if ( connection != null )<br />
         return connection;<br />
      try {<br />
         connection = DriverManager.getConnection(<br />
            dbUrl, dbUser, dbPassword);<br />
      } catch( SQLException e ) {<br />
         System.err.println(<br />
            "Cannot connect to database: "<br />
               + "check that Access is running and  "<br />
               + "the Hotel Database database exists." );<br />
      }<br />
      return connection;<br />
   }<br />
public ResultSet getAllCustomerInfo() {<br />
                ResultSet rs = null;<br />
                try {<br />
                        Statement statement =<br />
                                getConnection().createStatement();<br />
                        String sql =<br />
                                "SELECT * FROM Customer";<br />
                    System.out.println( sql );<br />
                        statement.executeQuery( sql );<br />
                        rs = statement.getResultSet();<br />
                } catch( SQLException e ) {<br />
                        System.out.println(<br />
                                "SQLException " + e.getMessage() );<br />
                } finally {<br />
                        return rs;<br />
                }<br />
        }<br />
 public void addCust( CustomerAccount account )<br />
                throws SQLException, DBopException<br />
        {<br />
                int nrows = 0;<br />
                Statement statement = getConnection().createStatement();<br />
                String sqlString ="INSERT INTO Customer VALUES ("<br />
                                + account.getCustomerName()<br />
                                + ",'"<br />
                                + account.getCustomerNo()<br />
                                + "','"<br />
                                + account.getCustomerType()<br />
                                + "',"<br />
                                + account.getCustomerComp()<br />
                                + ",'"<br />
                                + account.getCustomerAddress()<br />
                                + "','"<br />
                                + account.getNationality()<br />
                                + "','"<br />
                                + account.getPassportNo()<br />
                                + "','"<br />
                                + account.getPassword()<br />
                                + ")";<br />
                       <br />
<br />
                nrows = statement.executeUpdate( sqlString );<br />
                if ( nrows != 1 )<br />
                {<br />
                        throw new DBopException(<br />
                                "Instead of 1 row, "<br />
                                        + nrows<br />
                                        + " were inserted." );<br />
                }<br />
                return;<br />
        }<br />
  public void updateCust( CustomerAccount account )<br />
                throws SQLException, DBopException<br />
        {<br />
                int nrows = 0;<br />
                Statement statement =<br />
                        getConnection().createStatement();<br />
                String sql =<br />
                        "UPDATE Customer SET "<br />
                        + "CustomerName = "<br />
                        + account.getCustomerName()<br />
                        + ", "<br />
                        //+ "CustomerNo = '"<br />
                        //+ account.getCustomerNo()<br />
                        //+ "', "<br />
                        + "CustomerType = '"<br />
                        + account.getCustomerType()<br />
                        + "', "<br />
                        + "CustomerComp = "<br />
                        + account.getCustomerComp()<br />
                        + ", "<br />
                        + "CustomerAddress = '"<br />
                        + account.getCustomerAddress()<br />
                        + "', "<br />
                        + "Nationality = '"<br />
                        + account.getNationality()<br />
                        + "', "<br />
                        + "PassportNo = '"<br />
                        + account.getPassportNo()<br />
                        + "', "<br />
                        + "Password = '"<br />
                        + account.getPassword()<br />
                        + " WHERE CustomerNo="+account.getCustomerNo();<br />
<br />
<br />
                System.out.println( sql );<br />
                nrows = statement.executeUpdate( sql );<br />
                if ( nrows != 1 )<br />
                {<br />
                        throw new DBopException(<br />
                                "Instead of 1 row, "<br />
                                        + nrows<br />
                                        + " were updated." );<br />
                }<br />
                return;<br />
        }<br />
  }<br />
<br />


A customer account java class for the customer account itself:
<br />
/*<br />
 * CustomerAccount.java<br />
 *<br />
 * Created on October 14, 2008, 10:11 PM<br />
 *<br />
 * To change this template, choose Tools | Template Manager<br />
 * and open the template in the editor.<br />
 */<br />
<br />
/**<br />
 *<br />
 * <br />
 */<br />
public class CustomerAccount extends Account<br />
{<br />
   public CustomerAccount(String cName, int cNo, String cType, String cComp, String cAdd, String n, String pNo, String tAgency, String pass)<br />
  {<br />
    super(cName, cNo, cType, cComp, cAdd, n, pNo, tAgency, pass);<br />
    <br />
  }<br />
  <br />
<br />
}<br />
<br />
<br />
A DBopexception java class for the exception:<br />
<br />
/*<br />
 * DBopException.java<br />
 *<br />
 * Created on October 14, 2008, 10:23 PM<br />
 *<br />
 * To change this template, choose Tools | Template Manager<br />
 * and open the template in the editor.<br />
 */<br />
<br />
/**<br />
 *<br />
 * <br />
 */<br />
<br />
public class DBopException extends Exception {<br />
<br />
   public DBopException() {<br />
      this("Could not complete AccountDB database operation");<br />
   }<br />
<br />
   public DBopException(String s) {<br />
      super(s);<br />
   }<br />
}<br />



And finally a register servlet:

<br />
/*<br />
 * Register.java<br />
 *<br />
 * Created on October 14, 2008, 8:51 PM<br />
 */<br />
import java.io.*;<br />
import java.text.*;<br />
import java.sql.*;<br />
import javax.servlet.*;<br />
import javax.servlet.http.*;<br />
import javax.servlet.jsp.*;<br />
<br />
/**<br />
 * <br />
 * @version 1.0<br />
 */<br />
<br />
public class Register extends HttpServlet<br />
{<br />
     private AccountDB accountDB;<br />
     private Connection connection;<br />
     private Statement statement;<br />
     private CustomerAccount customerAccount;<br />
     private String query;<br />
<br />
  // set up database connection and create SQL statement<br />
     public void init( ServletConfig config ) throws ServletException<br />
     {<br />
     // attempt database connection<br />
<br />
          accountDB = new AccountDB();<br />
          accountDB.getConnection();<br />
     }  // end of init method<br />
<br />
  // process survey response<br />
     protected void doPost( HttpServletRequest request,<br />
                       HttpServletResponse response )<br />
     throws ServletException, IOException<br />
     {<br />
     // set up response to client<br />
        response.setContentType( "text/html" );<br />
        PrintWriter out = response.getWriter();<br />
<br />
<br />
     // start XHTML document<br />
        out.println( "" );<br />
<br />
        out.println( "                  "XHTML 1.0 Strict//EN\" \"http://www.w3.org" +<br />
                   "/TR/xhtml1/DTD/xhtml1-strict.dtd\">" );<br />
<br />
        out.println(<br />
                   "<html xmlns="\"http://www.w3.org/1999/xhtml\"">" );<br />
<br />
<br />
<br />
     // head section of document<br />
        out.println( "<head>" );<br />
<br />
     // read response by customer<br />
        String CustomerName, CustomerType, CustomerComp, CustomerAddress, Nationality, PassportNo, TravelAgency, Password;<br />
       <br />
       <br />
         CustomerName = request.getParameter( "CustomerName" );<br />
         int CustomerNo = Integer.parseInt(request.getParameter("CustomerNo"));<br />
         CustomerType = request.getParameter("CustomerType");<br />
         CustomerComp = request.getParameter("CustomerComp");<br />
         CustomerAddress = request.getParameter ("CustomerAddress");<br />
         Nationality = request.getParameter ("Nationality");<br />
         TravelAgency = request.getParameter ("TravelAgency");<br />
         PassportNo = request.getParameter("PassportNo");<br />
         Password = request.getParameter("Password");<br />
<br />
            <br />
          // attempt to process the account information<br />
         try<br />
         {<br />
            if (CustomerName.equals( "" ) ||<br />
            CustomerType.equals( "" ) ||<br />
            Password.equals("")) <br />
            {<br />
                out.println( "<h3> Please click the back " +<br />
                          "button and fill in all " +<br />
                          "fields.</h3>" );<br />
                out.close();<br />
                return;<br />
            }    <br />
            else<br />
                {<br />
                customerAccount = new CustomerAccount(CustomerName,<br />
                                    CustomerNo,<br />
                                    CustomerType,<br />
                                    CustomerComp,<br />
                                    CustomerAddress,<br />
                                    Nationality,<br />
                                    PassportNo,<br />
                                    TravelAgency,<br />
                                    Password);<br />
                accountDB.addCust(customerAccount);<br />
             }<br />
            out.println( "<title>Thank you!</title>" );<br />
            out.println( "</head>" );<br />
<br />
             out.println( "<body>" );<br />
            out.println( "<p>Thank you account created." );<br />
<br />
             // end XHTML document<br />
            out.println( "</p></body></html>" );<br />
            out.close();<br />
<br />
        }<br />
       <br />
      // if database exception occurs, return error page<br />
           catch ( SQLException sqlException )<br />
           {<br />
              sqlException.printStackTrace();<br />
              out.println( "<title>Error</title>" );<br />
              out.println( "" );<br />
              out.println( "<body><p>SQLException: Database error occurred. " );<br />
              out.println( "Try again later.</p></body>" );<br />
              out.close();<br />
           }   <br />
         catch(DBopException dbException)<br />
           {<br />
              dbException.printStackTrace();<br />
              out.println( "<title>Error</title>" );<br />
              out.println( "" );<br />
              out.println( "<body><p>DBopException: Database error occurred. " );<br />
              out.println( "Try again later.</p></body>" );<br />
              out.close();<br />
           }<br />
       <br />
     }  // end of doPost method<br />
<br />
  // close SQL statements and database when servlet terminates<br />
     public void destroy()<br />
     {<br />
     // attempt to close statements and database connection<br />
        try {<br />
           statement.close();<br />
           connection.close();<br />
        }<br />
<br />
        // handle database exceptions by returning error to client<br />
           catch( SQLException sqlException ) {<br />
              sqlException.printStackTrace();<br />
           }<br />
     }<br />
<br />
  } // end class RegisterServlet<br />


Now the problem is when the project runs and I have inserted all the data once I submit it says SQLException..I have tried other ways but cant find a solution for it..could it be that I have too many classes? IS there a better way to achieve what I want?

Any help would be appreciated.

PS. Ignore the comments in the code as they indicate the wrong things.
RantRe: SQL exception Pin
toxcct14-Oct-08 21:25
toxcct14-Oct-08 21:25 
QuestionPROCES TRASNFERRING FROM ONE CLIENT TO ANOTHER CLIENT Pin
madhump11-Oct-08 22:27
madhump11-Oct-08 22:27 
QuestionProcess tranferring from one client to another client Pin
madhump9-Oct-08 21:13
madhump9-Oct-08 21:13 
Questionadding new library to netbeans Pin
Areff8-Oct-08 12:42
Areff8-Oct-08 12:42 
QuestionConvert Fortran program to Java Pin
Trupti Mehta7-Oct-08 20:59
Trupti Mehta7-Oct-08 20:59 
AnswerRe: Convert Fortran program to Java Pin
toxcct8-Oct-08 1:03
toxcct8-Oct-08 1:03 
QuestionHelp to set apache tomcat 6.0 to netbeans Pin
hamid zamani7-Oct-08 2:12
hamid zamani7-Oct-08 2:12 
AnswerRe: Help to set apache tomcat 6.0 to netbeans Pin
Pedram Behroozi8-Oct-08 7:08
Pedram Behroozi8-Oct-08 7:08 
QuestionCode Optimization Pin
TannerB6-Oct-08 12:02
TannerB6-Oct-08 12:02 
AnswerRe: Code Optimization Pin
toxcct6-Oct-08 21:26
toxcct6-Oct-08 21:26 
GeneralRe: Code Optimization Pin
TannerB7-Oct-08 7:04
TannerB7-Oct-08 7:04 
GeneralRe: Code Optimization Pin
toxcct7-Oct-08 7:11
toxcct7-Oct-08 7:11 
AnswerRe: Code Optimization Pin
douss7-Oct-08 23:19
douss7-Oct-08 23:19 
GeneralRe: Code Optimization Pin
toxcct8-Oct-08 0:34
toxcct8-Oct-08 0:34 
QuestionHelp with digest message using md5 algrothim Pin
omar el halwagy6-Oct-08 4:44
omar el halwagy6-Oct-08 4:44 
QuestionHow to debug MSJVM in Eclipse Pin
utnqbao6-Oct-08 0:50
professionalutnqbao6-Oct-08 0:50 
AnswerRe: How to debug MSJVM in Eclipse Pin
toxcct6-Oct-08 1:37
toxcct6-Oct-08 1:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.