1

我将数据库中的结果存储到 JSONObject 中。我基本上希望 JSONObject 有 personIds,并且对于每个 personId,都有该人购买的汽车的所有 ID 的列表(汽车 ID),以及每辆汽车的购买日期。我有下面的代码,但我只得到一行的结果。我对循环很糟糕,显然我在这里遗漏了一点。请建议我在这里可能做错了什么,以及为什么我没有得到结果。只是为了了解输出,我为每个 personId 提供了超过 15 个 carId,并且每个 carId 都有一个购买日期。

public static JSONObject fetchPersonCarInfo(String person) throws SQLException
    {       
        JSONObject personCarInfoObj =  new JSONObject();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet rs = null;        
        try {
            connection = DriverManager.getConnection("jdbc:myDriver:myDatabase",username,password);
            statement = connection.prepareStatement("SELECT personId, carId, purchase_date FROM carz WHERE person = ?");
            statement.setString(1, person);
            rs = statement.executeQuery();          
            while(rs.next()) {                  
                if(personCarInfoObj.containsKey(rs.getString("personId")) {
                    personCarInfoObj.get(rs.getString("personId"));
                    personCarInfoObj.get(rs.getString("carId"));
                    personCarInfoObj.get(rs.getString("purchaseDate"));
                    } else {
                    personCarInfoObj = new JSONObject();
                    personCarInfoObj.put("personId",(new String(rs.getString("personId"))));
                    personCarInfoObj.put("deviceId",(new String(rs.getString("carId"))));
                    personCarInfoObj.put("deviceCheckinTime",(new Date(rs.getTimestamp("purchaseDate")).getTime())); //not sure how to get timestamp column
                }
            }           
        } finally{
            statement.close();
            rs.close();
            connection.close();
        }       
        return personCarInfoObj;
    }
4

1 回答 1

1

请阅读我添加到代码中的注释,以了解我对逻辑所做的更改。如果您不明白任何部分,请随时询问。

public static JSONObject fetchPersonCarInfo(String person) throws SQLException {

    JSONObject personCarInfoObj = new JSONObject();
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet rs = null;
    try {
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "username", "password");
        statement = connection.prepareStatement("SELECT personId, carId, purchaseDate FROM person WHERE person = ?");
        statement.setString(1, person);
        rs = statement.executeQuery();
        JSONArray carsArray = null;
        while (rs.next()) {
            // lookup if we have an entry for personId already in personCarInfoObj
            if (personCarInfoObj.containsKey(rs.getString("personId"))) {
                // build a new object for car info                
                JSONObject personCarObj = new JSONObject();
                carsArray = (JSONArray) personCarInfoObj.get(rs.getString("personId"));
                personCarObj.put("deviceId", (new String(rs.getString("carId"))));
                personCarObj.put("deviceCheckinTime", new Date(rs.getTimestamp("purchaseDate").getTime())); //not sure how to get timestamp column
                // this will append new car info object to the array with key rs.getString("personId"))
                carsArray.add(personCarObj);
                personCarInfoObj.put(rs.getString("personId"), carsArray);
            } else {                    
                carsArray = new JSONArray();
                JSONObject personCarObj = new JSONObject();
                personCarObj.put("deviceId", (new String(rs.getString("carId"))));
                personCarObj.put("deviceCheckinTime", new Date(rs.getTimestamp("purchaseDate").getTime())); //not sure how to get timestamp column
                carsArray.add(personCarObj);
                // store all the cars purchased against that personId in personCarInfoObj
                personCarInfoObj.put(new String(rs.getString("personId")), carsArray);
            }
        }
    } finally {
        statement.close();            
        connection.close();
    }
    return personCarInfoObj;
}

另外,我建议阅读有关构建 JSON 对象的信息。

干杯!

于 2013-11-05T07:39:51.903 回答