私は Java の学生として小さな個人プロジェクトに取り組んでおり、Mysql データベースを表示する簡単な Web ページを作成するように依頼されました。mySql では、日付を DATE 型で宣言しました。以下のスクリーンショットを参照してください。
以下の Java コードは、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"));
}
以下は、私の Web ページの出力のスクリーンショットです。Web ページの DepartureDate と ReturnDate の形式は、DB と同じ形式を反映する必要があります
これは私の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>
コンストラクターとゲッターを使用した宛先クラス
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;
}
}