Note that there are some explanatory texts on larger screens.

plurals
  1. POLooping through database ResultSet
    primarykey
    data
    text
    <p>I have a snippet of Java code here which is supposed to retrieve results from a database query and the ResultSet is supposed to iterate through the values in order to retrieve certain API data for each entry of the ResultSet. However, the problem is that I can retrieve API data for only the first entry of my ResultSet.</p> <p>This code works exactly as expected and returns all my database entries.</p> <pre><code>try { ResultSet rs; rs = stat.executeQuery("select * from schedule"); while (rs.next()) { model.addRow(new Object[]{rs.getString("SHOW"), rs.getString("SEASON")}); } } catch (Exception e) { console.append(e.getMessage() + '\n'); } </code></pre> <p>However, this code returns only the first entry.</p> <pre><code>try { ResultSet rs = stat.executeQuery("select * from schedule"); while (rs.next()) { String show = rs.getString("SHOW"); String season = rs.getString("SEASON"); String api_url = "&lt;API_URL&gt;/" + show + "/" + season; URL url = new URL(api_url); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = con.getResponseCode(); if (responseCode == 200) { conn_stat.setText("Connection Status : OK"); } else { conn_stat.setText("Connection Status : ERR"); } BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); String s = response.toString(); JsonArray json = JsonArray.readFrom(s); for (int i = 0; i &lt; json.size(); i++) { JsonObject show_json = json.get(i).asObject(); int episode = show_json.get("episode").asInt(); String date = show_json.get("first_aired_iso").asString(); String title = show_json.get("title").asString(); String date_formatted = date.substring(0, date.indexOf("T")); SimpleDateFormat original = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat target = new SimpleDateFormat("dd-MMM-yyyy"); Date unformatteddate = original.parse(date_formatted); String dateStart = target.format(unformatteddate); Date curr_date = new Date(); String dateStop = target.format(curr_date); Date d1 = null; Date d2 = null; d1 = target.parse(dateStart); d2 = target.parse(dateStop); long diff = d2.getTime() - d1.getTime(); long diffDays = diff / (24 * 60 * 60 * 1000); if (diffDays &lt; 0) { alert_model.addRow(new Object[]{show + " - " + episode, title, dateStart}); } } } } catch (Exception e) { console.append(e.getMessage() + '\n'); } </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.
    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