我正在尝试从数据库中检索数据。它是 PHP MyAdmin 数据库 (MySQL)。这是我写的代码:
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;
}
}
上面是我有照片的类(class)。
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。
关于为什么它为空的任何提示?
我接受提示而不是直接答案,这样我就可以尝试单独修复。请随意给我建议,但如果你确实想给出答案,我会接受。
先谢谢了,我已经找了3个小时的答案了,我很绝望哈哈!
最佳答案
您只是从数据库中选择了记录,但尚未检索它。
因此,尝试迭代 ResultSet
并从数据库中获取记录,如下方法 retrieveData
rs = ps.executeQuery();
// retrieve records from database here
if(rs.next()) {
idPicture = rs.getInt(1); // here 1 is column index of column pictureId
longitude = rs.getString(2); // here 2 is column index of string longitude
latitude = rs.getString(3);
status = rs.getInt(4);
path = rs.getString(5);
timestamp = rs.getTimestamp(6);
pic = new Pictures(idPicture, longitude, latitude, status, path, timestamp);
} else {
// there is no record with id 1.
}
希望这会有所帮助。
关于java - 数据检索 - 带有 Eclipse 的 JDBC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45148971/