データベースからデータを取得しようとしています。 PHP MyAdminデータベース(MySQL)です。ここには私が書いたコードがあります:データ取得 - EclipseでのJDBC
package dto;
import java.sql.Timestamp;
public class Pictures {
int idPicture;
String longitude;
String latitude;
int status;
String path;
Timestamp timestamp;
/*
* Constructor for a picture
*/
public Pictures(int idPicture,
String longitude,
String latitude,
int status,
String path,
Timestamp timestamp){
this.idPicture = idPicture;
this.longitude = longitude;
this.latitude = latitude;
this.status = status;
this.path = path;
this.timestamp = timestamp;
}
public int getIdPicture() {
return idPicture;
}
public void setIdPicture(int idPicture) {
this.idPicture = idPicture;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
}
上記は私の写真があるクラスです。
package dao;
import java.sql.*;
import java.util.ArrayList;
import dto.Pictures;
public class Storingdb {
private static final String url = "jdbc:mysql://localhost/internship";
private static final String username = "root";
private static final String password = "";
public Storingdb() {
}
/*
* Connection method
*/
private Connection connect() {
Connection connexion;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException cnfe) {
System.err.println("Error loading driver: " + cnfe);
}
// Connection to the database
try {
connexion = DriverManager.getConnection(url, username, password);
return connexion;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public Pictures retrieveData() {
Pictures pic = null;
int idPicture = 0;
String longitude = null;
String latitude = null;
int status = 0;
String path = null;
Timestamp timestamp = null;
Connection connexion = connect();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = connexion.prepareStatement("SELECT * FROM storing WHERE id=1");
rs = ps.executeQuery();
pic = new Pictures(idPicture, longitude, latitude, status, path, timestamp);
while (rs.next()) {
pic.setIdPicture(rs.getInt(1));
longitude = rs.getString(2);
latitude = rs.getString(3);
status = rs.getInt(4);
path = rs.getString(5);
timestamp = rs.getTimestamp(6);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
ps.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
connexion.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return pic;
}
}
上記はDAOクラスです。
<div>
<%
Storingdb dao = new Storingdb();
//ArrayList<Pictures> file = new ArrayList<Pictures>();
//file = dao.retrieveData();
Pictures pic;
pic = dao.retrieveData();
String empty = "";
if(pic.getStatus() == 0) {
empty = "Empty";
} else {
empty = "Taken";
}
%>
<table class="table table-bordered">
<thead>
<tr>
<th>Parking Space ID</th>
<th>Longitude</th>
<th>Latitude</th>
<th>Status :</th>
<th>Update time :</th>
</tr>
</thead>
<tbody>
<tr>
<td><%= pic.getIdPicture() %></td>
<td><%= pic.getLongitude() %></td>
<td><%= pic.getLatitude() %></td>
<td><%= empty %></td>
<td><%= pic.getTimestamp() %></td>
</tr>
</tbody>
</table>
</div>
ここに私のJSPのHTMLコードがあります。
私のテーブルは、iD /ステータス以外のすべてについてnullを返しています。 私はなぜそれが理解できません。
私のデータベースでは、iDはint(11)、自動インクリメントです。 longitudeはVARCHAR(32) です。latitudeはVARCHAR(32)です。 ステータスはintです。 pathはVARCHAR(32)です。 timestampはデータの追加/更新時に更新されるTIMESTAMPです。
なぜnullであるかに関するヒント?
私は、直接回答ではなくヒントを受け取り、私は一人で解決しようとすることができます。私に助言を与えることは自由であるが、あなたがそれを与えたい場合は答えを受け入れる。
前もってありがとう、私は3時間の答えを探していました、そして、私は絶望的なハハです!
ありがとう@ELITE!それはたくさんの助けになりました! エラーが発生しました。 これで、ピクチャのリストを作成したい場合は、ArrayListを作成して、 ArrayList file = new ArrayList()を作成できます。 file.add(pic); while(rs.next())の最後に があり、PreparedStatementの "WHERE id = 1"を削除する必要がありますか? –
はい............. – ELITE