简体   繁体   English

如何将MySQL表数据插入JSONArray?

[英]How do I insert MySQL Table data into an JSONArray?

I'm trying to get some data from a MySQL table and insert this data into an JSONArray. 我正在尝试从MySQL表中获取一些数据,并将此数据插入JSONArray。

Below my Code: 在我的代码下方:

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;
    }

In my table I have two occurrences once sql query is executed but in the resulting array I get two times the last occurrence in the Table. 在我的表中,一旦执行sql查询,便有两次出现,但是在结果数组中,表中最后一次出现的次数是我的两倍。 I guess it's while's fault. 我想这是一段时间的错。 Do you guys know how to get both occurrences in the JSONArray? 你们知道如何在JSONArray中获得这两种情况吗? Thank you 谢谢

In the first iteration, the key-values are added firstly. 在第一次迭代中,首先添加键值。 But after the first iteration, you are overriding the mapped values for each keys in the object. 但是在第一次迭代之后,您将覆盖对象中每个键的映射值。 Because, you add the same object multiple times overriding its entries. 因为,您添加同一对象多次覆盖其条目。 You must create a new jsonObject with each iteration. 您必须在每次迭代中创建一个新的jsonObject。 So the 所以

JSONObject obj = new JSONObject(); JSONObject obj =新的JSONObject();

must be in the while loop. 必须在while循环中。 like 喜欢

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;
}

Moreover, you don't need to keep 'index' separately to specify place of the element to be added. 而且,您无需单独保留“索引”来指定要添加的元素的位置。 It is already added by default to the end of the array. 默认情况下已将其添加到数组的末尾。 So, it is better to add element like: array.add(obj); 因此,最好添加如下元素: array.add(obj); if the new element will be added to the end of the array. 如果新元素将添加到数组的末尾。

You need to move your object instantiation into the loop, like this: 您需要将对象实例化移动到循环中,如下所示:

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

You need create a new object in each iteration, because you are overwriting that object- 您需要在每次迭代中创建一个新对象,因为您要覆盖该对象,

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++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM