我试图从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中同时出现这两种情况吗? 谢谢
答案 0 :(得分:2)
在第一次迭代中,首先添加键值。但是在第一次迭代之后,您将覆盖对象中每个键的映射值。因为,您多次添加相同的对象会覆盖其条目。您必须在每次迭代时创建一个新的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;
}
此外,您不需要单独保留'index'来指定要添加的元素的位置。它已默认添加到数组的末尾。因此,如果将新元素添加到数组的末尾,最好添加如array.add(obj);
之类的元素。
答案 1 :(得分:2)
您需要将对象实例化移动到循环中,如下所示:
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;
}
答案 2 :(得分:1)
您正在尝试将每个元素放入数组中使用相同的JSONObject(相同的引用),因此您只需在循环中覆盖其值。
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<div class="col-lg-12">
<label>Username</label>
<input type="text" class="form-control">
<div class="field-help"> Choose a username that contains only letters and numbers, or use your email address. </div>
</div>
<div class="col-lg-12">
<label>Password</label>
<input type="password" class="form-control">
<div class="field-help"> It's allways good to have one. </div>
</div>
答案 3 :(得分:1)
您需要在每次迭代中创建一个新对象,因为您要覆盖该对象 -
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++;
}