作为一名Java学生,我正在做一个小型的个人项目,我被要求创建一个显示Mysql数据库的简单网页。在mySql中,我在DATE类型中声明了日期。
见下面的screanShot。
下面的java代码显示了如何从数据库中检索数据。

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"));
    }

下面是我网页上输出的屏幕截图。
我网页上的DepartureDate和ReturnDate格式应与数据库中的格式相同
这是我的JSP代码
<%@ 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>

带构造函数和getter的目标类
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;
    }


}

最佳答案

您使用的是javax.sql.Date还是java.util.Date。请记住,EL标记使用toString()方法,可能需要对日期进行如下线程所示的格式设置:
Convert java.util.Date to String
另一种方法是按以下方式使用JSTL标记:

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

为此,您必须将JSTL库添加到JavaEE应用程序中,如下所示:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

注意,这与JSTL核心不同。

关于java - SQL仅返回年份,而不返回日期或月份,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15701532/

10-09 15:55
查看更多