我正在尝试从MySQL表中获取一些数据并将此数据插入到JSONArray中。
在我的代码下面:
public static JSONArray get_inactive_bookings(String username) {
JSONArray array = new JSONArray();
JSONObject obj = new JSONObject();
Connection conn = null;
ResultSet rs = null;
int index = 0;
int user_id = Users.get_user_id(username);
String sql = "select * from bookings where user_id =" + user_id +
" and Effettiva_Restituzione is not null";
try {
conn = Utilities.connect();
Statement st = conn.createStatement();
rs = st.executeQuery(sql);
//This is where I think problem shows up
while (rs.next()) {
obj.put("Booking_ID", rs.getInt("booking_id"));
obj.put("Book_Title", Books.get_title(rs.getInt("book_id")));
obj.put("Booking_Date", rs.getString("Data_Prenotazione"));
obj.put("Return_Date", rs.getString("Data_Restituzione"));
array.add(index, obj);
index++;
}
} catch (SQLException ecc) {
System.out.println("ERROR: " + ecc.getMessage());
} finally {
Utilities.disconnect(conn);
}
return array;
}
在我的表中,一旦执行sql查询,我有两次出现,但在结果数组中,我得到表中最后一次出现的两次。我想这是有问题的。你们知道如何在JSONArray中同时出现这两种情况吗?谢谢
在第一次迭代中,首先添加键值。但是在第一次迭代之后,您将覆盖对象中每个键的映射值。因为,您多次添加相同的对象会覆盖其条目。您必须在每次迭代时创建一个新的jsonObject。所以
JSONObject obj = new JSONObject();
必须在while循环中。喜欢
public static JSONArray get_inactive_bookings(String username) {
JSONArray array = new JSONArray();
//JSONObject obj = new JSONObject(); -> this line must be in while loop
Connection conn = null;
ResultSet rs = null;
int index = 0;
int user_id = Users.get_user_id(username);
String sql = "select * from bookings where user_id =" + user_id +
" and Effettiva_Restituzione is not null";
try {
conn = Utilities.connect();
Statement st = conn.createStatement();
rs = st.executeQuery(sql);
//This is where I think problem shows up
while (rs.next()) {
JSONObject obj = new JSONObject(); // moved to here
obj.put("Booking_ID", rs.getInt("booking_id"));
obj.put("Book_Title", Books.get_title(rs.getInt("book_id")));
obj.put("Booking_Date", rs.getString("Data_Prenotazione"));
obj.put("Return_Date", rs.getString("Data_Restituzione"));
array.add(index, obj);
index++;
}
} catch (SQLException ecc) {
System.out.println("ERROR: " + ecc.getMessage());
} finally {
Utilities.disconnect(conn);
}
return array;
}
此外,您不需要单独保留“索引”来指定要添加的元素的位置。它已默认添加到数组的末尾。因此,如果将新元素添加到数组的末尾,最好添加如下元素:array.add(obj);
。
您需要将对象实例化移动到循环中,如下所示:
public static JSONArray get_inactive_bookings(String username) {
JSONArray array = new JSONArray();
Connection conn = null;
ResultSet rs = null;
int index = 0;
int user_id = Users.get_user_id(username);
String sql = "select * from bookings where user_id =" + user_id +
" and Effettiva_Restituzione is not null";
try {
conn = Utilities.connect();
Statement st = conn.createStatement();
rs = st.executeQuery(sql);
//This is where I think problem shows up
while (rs.next()) {
JSONObject obj = new JSONObject();
obj.put("Booking_ID", rs.getInt("booking_id"));
obj.put("Book_Title", Books.get_title(rs.getInt("book_id")));
obj.put("Booking_Date", rs.getString("Data_Prenotazione"));
obj.put("Return_Date", rs.getString("Data_Restituzione"));
array.add(index, obj);
index++;
}
} catch (SQLException ecc) {
System.out.println("ERROR: " + ecc.getMessage());
} finally {
Utilities.disconnect(conn);
}
return array;
}
您正在尝试将每个元素放入数组中使用相同的JSONObject(相同的引用),因此您只需在循环中覆盖其值。
JSONObject obj = new JSONObject(); //put this inside your while loop
您需要在每次迭代中创建一个新对象,因为您要覆盖该对象 -
while (rs.next()) {
JSONObject obj = new JSONObject();
obj.put("Booking_ID", rs.getInt("booking_id"));
obj.put("Book_Title", Books.get_title(rs.getInt("book_id")));
obj.put("Booking_Date", rs.getString("Data_Prenotazione"));
obj.put("Return_Date", rs.getString("Data_Restituzione"));
array.add(index, obj);
index++;
}