存储db的问题导致JSONObject

时间:2013-11-05 07:08:52

标签: java json

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

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

1 个答案:

答案 0 :(得分: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对象的内容。

干杯!