1

I'm working on a small personal project as a Java student and i've been asked to create a simple webpage that displays a Mysql database. In mySql, i declared my dates in type DATE. See screanShot below.

enter image description here

The java code below shows how i retrieve the datas from my DB.

private Destination resultSetRowToCursist(ResultSet resultSet)
            throws SQLException {
        return new Destination (resultSet.getInt("CountryID"),
                resultSet.getString("Destination"),
                resultSet.getDate("DepartureDate"),
                resultSet.getDate("ReturnDate"), 
                resultSet.getInt("Price"),
                resultSet.getInt("AvailableSeats"));
    }

Below is the screanshot of the output on my webpage. DepartureDate and ReturnDate format on my webpage should be reflecting the same format as in the DB

enter image description here

This is my JSP code

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="styles/default.css">

    <title>Travels</title>
    </head>
    <body>

    <table border=1>

            <tr>

                <th>CountryId</th>
                <th>Country</th>
                <th>DepartureDate</th>

                <th>ReturnDate</th>

                <th>Price</th>
                <th>AvailableSeats</th>

    </tr>

            <c:forEach var="destinationArrayListItem" items="${DestinationArrayList}">

                <tr>

                    <td>${destinationArrayListItem.countryID}</td>
                    <td>${destinationArrayListItem.destination}</td>
                    <td>${destinationArrayListItem.departureDate}</td>
                    <td>${destinationArrayListItem.returnDate}</td>
                    <td>${destinationArrayListItem.price}</td>
                    <td>${destinationArrayListItem.availableSeats}</td>


                </tr>

            </c:forEach>

        </table>



        <br />

        <c:url var="index" value="/IndexServlet" />

        <a class="HPbutton" href="${index}">Home Page</a>


    </body>
    </html>

My Destination class with COnstructor and getters

import java.io.Serializable;
import java.sql.Date;

public class Destination implements Serializable {

    private static final long serialVersionUID = 1L;

    private int countryID;

    private String destination;

    private  Date departureDate;
    private  Date returnDate;
    private  int price;
    private  int availableSeats;


    public Destination () {
          this.countryID=0;;

          this.destination="geen";

          this.departureDate= null;
          this.returnDate= null;
          this.price=0;
          this.availableSeats=0;


    }

    public Destination(int countryID, String destination, Date departureDate,
            Date returnDate, int price, int availableSeats) {
        this.countryID = countryID;
        this.destination = destination;
        this.departureDate = departureDate;
        this.returnDate = returnDate;
        this.price = price;
        this.availableSeats = availableSeats;


    }

    public int getCountryID() {
        return countryID;
    }

    public void setCountryID(int countryID) {
        this.countryID = countryID;
    }

    public Date getDepartureDate() {
        return departureDate;
    }

    public void setDepartureDate(Date departureDate) {
        this.departureDate = departureDate;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public Date getReturnDate() {
        return returnDate;
    }

    public void setReturnDate(Date returnDate) {
        this.returnDate = returnDate;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getAvailableSeats() {
        return availableSeats;
    }

    public void setAvailableSeats(int availableSeats) {
        this.availableSeats = availableSeats;
    }


}
AchillesVan
  • 4,156
  • 3
  • 35
  • 47
  • how is the web page rendering the object? – Preet Sangha Mar 29 '13 at 10:34
  • Can you please show the `Bestemming` constructor declaration? And how are you showing the Date later? – m0skit0 Mar 29 '13 at 10:36
  • Actually Bestemming is Destination in dutch. I just made a correction. please review the code again – AchillesVan Mar 29 '13 at 10:38
  • @Rahoul Still: show the constructor code and the getters for the dates – Mark Rotteveel Mar 29 '13 at 10:47
  • please show the constructor. Is returndate in Destination a date object? – vishnu viswanath Mar 29 '13 at 10:49
  • Its there. i just posted it – AchillesVan Mar 29 '13 at 10:50
  • You have to be aware the Date Object internally has only a number. It is impossible to express only a year with such an object. However, when formatting the date into a String you're free to select what data you want. So I'd recomment you to re-think where your problem is located. A first step might be debug the consturctor and to see what comes there, and then to follow the data through the system. – Bernd Ebertz Mar 29 '13 at 10:53

1 Answers1

1

Are you using javax.sql.Date or java.util.Date. Remebering that EL tags use the toString() method, it may be worth formatting the date as shown in the following thread:

Convert java.util.Date to String

Another way to do this is to use JSTL tags in the following manner:

<td><fmt:formatDate value="${destinationArrayListItem.departureDate}" pattern="MM/yyyy" /></td>
<td><fmt:formatDate value="${destinationArrayListItem.returnDate}" pattern="MM/yyyy" /></td>

To do this, you will have to add the JSTL library to your JavaEE application as follows:

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

Note, this is different from the JSTL core.

Community
  • 1
  • 1
blackpanther
  • 10,998
  • 11
  • 48
  • 78
  • I'm using import java.sql.Date; – AchillesVan Mar 29 '13 at 10:52
  • That's fine and it's good that you're using sql.Date. You may want to look at the JSTL library with JSPs. It can make date formatting and other things a lot more easier and it's fairly easy to learn and implement. – blackpanther Mar 29 '13 at 11:00
  • Ok, i'll try and i'll come back to provide a feedback – AchillesVan Mar 29 '13 at 11:10
  • Sorry, yes, you've got JSTL core in your code. I didn't realise it before. That means that you should be able to insert the code in my answer. If not, then please let me know. – blackpanther Mar 29 '13 at 11:11
  • I'm getting an yellow warning in my JSP when i use your fmt:format: error says: number format value does not support runtime expressions? – AchillesVan Mar 29 '13 at 11:29
  • You must import the following JSTL library (additional to the JSTL core): <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> As for yellow warnings, don't worry about them too much as they are for HTML checking. JSPs are different – blackpanther Mar 29 '13 at 11:31
  • Thank you for your help but this doesnt work. Returning HTTP status 500 : According to TLD or attribute directive in tag file, attribute value does not accept any expressions – AchillesVan Mar 29 '13 at 12:11
  • That is very strange because the taglib import in my answer should work. Please note that the taglib import in my previous comment has a semi-colon that should not be there. – blackpanther Mar 29 '13 at 12:43
  • Now the yellow warning is gone after removing the semicolon (great) but when i launch my app, tomcat gives me a : HTTP Status 500 - An exception occurred processing JSP page /WEB-INF/JSP/showDestination.jsp at line 45 . Fyi, Line 45 is the line with the fmt tags. – AchillesVan Mar 29 '13 at 13:50
  • You may want to see this thread: http://stackoverflow.com/questions/6231288/jstl-format-tag – blackpanther Mar 29 '13 at 14:30
  • It's working now and I could not tell you how I've fixed it. I just created a new DB in mysql and BOUM !!. Thank you friend. NB: I did not use the fmt jstl tag – AchillesVan Mar 30 '13 at 11:26