2

I wrote a simple form and managed bean to store the data submitted to SQL server. downloaded sqljdbc and got it installed. The following occurs:

javax.servlet.ServletException: Cannot format given Object as a Date

unsure what is really happening...

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.prime.com.tr/ui"
  xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>Reservation Request</title>
</h:head>
<body>
<h:form>
    <h2>Event Details:</h2>
    <table cellspacing="10">
        <tr>
            <td>
                Event Title:
            </td>
            <td>
        <p:inputText id="eventTitle" value="#{formData.eventTitle}"/>
            </td>
        </tr>
    </table>
    <table>
        <tr>
            <td>
                <p:tab title="startDate">
                     <h:panelGrid columns="2" cellpadding="10">
                      <label>Start Date</label>
                      <p:calendar value="#{formData.startDate}}" required="true" />
                      <h:message for="startDate" />
                     </h:panelGrid>
                    </p:tab>
            </td>
            <td>
                 <p:tab title="endDate">
                     <h:panelGrid columns="2" cellpadding="10">
                      <label>End Date</label>
                      <p:calendar value="#{formData.endDate}" required="true" />
                      <h:message for="endDate" />
                     </h:panelGrid>
                    </p:tab>
            </td>
        </tr>
        <tr>
            <td>
                Request Type/Purpose:
            </td>
        </tr>
        <tr>
            <td>

                <h:selectOneMenu id="purposeOption"
                required="true"
                value="#{formData.requestType}">
                <f:selectItem
                    itemValue="#{formData.projectOption}"
                    itemLabel="Project"/>
                <f:selectItem
                    itemValue="#{formData.trainingOption}"
                    itemLabel="Training"/>
                </h:selectOneMenu>
            </td>
        </tr>
    </table>
    <table cellspacing="10">
        <tr>
            <td>
                Workstations Required:
            </td>
            <td>
        <p:inputText value="#{formData.terminalsRequired}" id="terminals"/>
            </td>
        </tr>
    </table>
    <br></br>
    <h2>Requester Information:</h2>
    <table cellspacing="10">
        <tr>
            <td>
                Last Name:
            </td>
            <td>
                <p:inputText value="#{formData.lastName}" id="lastName"/>
            </td>
             <td>
                First Name:
            </td>
            <td>
                <p:inputText value="#{formData.firstName}" id="firstName"/>
            </td>
             <td>
                M.I:
            </td>
            <td>
            <p:inputText value="#{formData.middleInitials}" id="middleInitials"/>
            </td>
        </tr>
        <tr>
            <td>
                Badge:
            </td>
            <td>
        <p:inputText value="#{formData.badgeNo}" id="badgeNo"/>
            </td>
            <td>
                Network ID:
            </td>
            <td>
            <p:inputText value="#{formData.networkID}" id="networkID"/>
            </td>
            <td>
                Telephone:
            </td>
            <td>
            <p:inputText value="#{formData.telephoneNo}" id="telephoneNo"/>
            </td>
        </tr>
        <tr>
            <td>
                Org Code:
            </td>
            <td>
        <p:inputText value="#{formData.orgCode}" id="orgCode"/>
            </td>
            <td>
                Org Name:
            </td>
            <td>
               <p:inputText value="#{formData.orgName}" id="orgname"/>
            </td>
        </tr>
    </table>
    <br></br>
    <table>
        <tr>
            <td>
                <b>Justification:</b>
            </td>
        </tr>
         <tr>
            <td>
         <p:inputTextarea value="#{formData.justification}" id="justification" cols="80" rows="10" />
            </td>
        </tr>
        <tr>
            <td>
        <h:commandButton value="Submit Data!" action="#{formData.storeTheData()}"/>
            </td>
        </tr>
    </table>
</h:form>
</body>
</html>

and the backing bean is:

package core.smd.classes;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.util.Date;
import java.sql.*;
/**
*
* @author 
*/
@ManagedBean
@SessionScoped
public class FormData {
//global variables
String eventTitle;
Date startDate;
Date endDate;
String requestType;
int terminalsRequired;
String lastName;
String firstName;
String middleInitials;
int badgeNo;
String networkID;
String telephoneNo;
String orgCode;
String orgName;
String trainingOption;
String projectOption;
String justification;
/*End of variables declaration*/

/** Creates a new instance of FormData */
public FormData() {
}

public int getBadgeNo() {
    return badgeNo;
}

public String getJustification() {
    return justification;
}

public void setJustification(String justification) {
    this.justification = justification;
}


public void setBadgeNo(int badgeNo) {
    this.badgeNo = badgeNo;
}

public String getProjectOption() {
    return projectOption;
}

public void setProjectOption(String projectOption) {
    this.projectOption = projectOption;
}

public String getTrainingOption() {
    return trainingOption;
}

public void setTrainingOption(String trainingOption) {
    this.trainingOption = trainingOption;
}



public Date getEndDate() {
    return endDate;
}

public void setEndDate(Date endDate) {
    this.endDate = endDate;
}

public String getEventTitle() {
    return eventTitle;
}

public void setEventTitle(String eventTitle) {
    this.eventTitle = eventTitle;
}

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 getMiddleInitials() {
    return middleInitials;
}

public void setMiddleInitials(String middleInitials) {
    this.middleInitials = middleInitials;
}

public String getNetworkID() {
    return networkID;
}

public void setNetworkID(String networkID) {
    this.networkID = networkID;
}

public String getOrgCode() {
    return orgCode;
}

public void setOrgCode(String orgCode) {
    this.orgCode = orgCode;
}

public String getOrgName() {
    return orgName;
}

public void setOrgName(String orgName) {
    this.orgName = orgName;
}

public String getRequestType() {
    return requestType;
}

public void setRequestType(String requestType) {
    this.requestType = requestType;
}

public Date getStartDate() {
    return startDate;
}

public void setStartDate(Date startDate) {
    this.startDate = startDate;
}

public String getTelephoneNo() {
    return telephoneNo;
}

public void setTelephoneNo(String telephoneNo) {
    this.telephoneNo = telephoneNo;
}

public int getTerminalsRequired() {
    return terminalsRequired;
}

public void setTerminalsRequired(int terminalsRequired) {
    this.terminalsRequired = terminalsRequired;
}

public void storeTheData(){
    //prepared statenebts
    PreparedStatement preStmt = null; 


    try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
        "xxxx;user=sa;password=xxxxx;";
    Connection con = DriverManager.getConnection(connectionUrl);
    String sqlQuery = "INSERT INTO Pending_Requests(Request_No, Request_Details, Network_ID, Request_Status) VALUES(?, ?, ?, ?)";
    preStmt = con.prepareStatement(sqlQuery);
    preStmt.setString(1, telephoneNo);
    preStmt.setString(2, eventTitle);
    preStmt.setString(3, networkID);
    preStmt.setString(4, eventTitle);
    preStmt.executeUpdate();
    con.commit();

    } catch (SQLException e) {
        System.out.println("SQL Exception: "+ e.toString());
    } catch (ClassNotFoundException cE) {
        System.out.println("Class Not Found Exception: "+ cE.toString());
    }
}
}

I know the data submitted isn't actually being stored fully, but I am just testing if SQL qorks and preparedStatement...hence storing only part of the submitted data. Thanks,

sys_debug
  • 3,883
  • 17
  • 67
  • 98
  • 4
    note: `#{formData.startDate}}` there's a double `}}` – Bozho Nov 16 '11 at 12:31
  • Could it be that `startDate` or `endDate` are `null`? I'd imagine that the framework would have trouble converting a null into a value for a calendar input... – Andrzej Doyle Nov 16 '11 at 12:32
  • Ok so the double curly brackets around the startDate was causing the issue...a typo ofcourse. But now when I enter the data and submit, nothing happens or stored into database. Any ideas? – sys_debug Nov 16 '11 at 12:48

1 Answers1

1

try using the date time converter.

<f:convertDateTime pattern="dd/MM/yyyy HH:mm" timeZone="GMT+5" />

add your own time zone ofcourse here is a working snippet

<p:calendar id="end" value="#{message.endDate}" mode="popup" pattern="dd/MM/yyyy HH:mm" size="17">
                                    <f:convertDateTime pattern="dd/MM/yyyy HH:mm" timeZone="GMT+5" />
                                </p:calendar>
Khizar
  • 2,288
  • 5
  • 32
  • 53
  • yes mate would have done that considering I actually processed the dates but am not. Am just storing the 3 fields as you may see from the code :) – sys_debug Nov 16 '11 at 12:56
  • this is great mate will keep it for later when dealing with the date. :D – sys_debug Nov 16 '11 at 13:00
  • ahhh u mean date is not your problem now, the submit is not working?? OR you do not have a problem anymore, the date was not being stored in the database previously and you have found out now that it was because you were not processing it ?? which one is it ?? – Khizar Nov 16 '11 at 13:05