0
我试图从我的数据库检索数据。它是PHP MyAdmin数据库(MySQL)。这里是我写的代码:数据检索 - JDBC与Eclipse
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),自动递增。 经度为VARCHAR(32) 纬度为VARCHAR(32) 状态为int(1) 路径为VARCHAR(32) timestamp是TIMESTAMP,在添加/更新数据时更新。
为什么它是空的任何提示?
我接受提示而不是直接回答,所以我可以尝试单独修复。随时给我建议,但如果你想给它的话我接受答案。
在此先感谢,我一直在寻找答案3小时,我绝望哈哈!
是啊谢谢@ELITE!这非常有帮助! 现在我明白我的错误了。 所以,如果我现在想制作一个图片列表,我可以创建一个ArrayList并写入 ArrayList file = new ArrayList (); file.add(pic); 在while(rs.next())的末尾,我需要在PreparedStatement中删除“WHERE id = 1”? –
是............. – ELITE