Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues storing db results in JSONObject
    primarykey
    data
    text
    <p>I am storing results from my database into a JSONObject. I basically want the JSONObject to have personIds, and for each personId, have the list of all the ids of the cars the person purchased (car id), and the purchase date for each of these cars. I have the below code, but I am only getting results of one row. I am terrible with loops, clearly I am missing a point here. Please suggest what I could be doing wrong here, and why I am not getting back the results. Just to give an idea of the output, I have over 15 carIds for each personId, and each of these carIds have a purchase date.</p> <pre><code>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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload