Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm little confused from what are you trying to achieve but i guess you need this:</p> <pre><code>select * from city as c inner join user as u on (c.ID = u.city_ID) where u.name = ?; </code></pre> <p>Then you need only execute this one query and you have desired results.</p> <p>But also as @dystroy mentioned if you want to use your approach you need at first fetch ids from User into some <code>List&lt;Integer&gt;</code> and then create some <code>List&lt;String&gt;</code> that will hold your queries or use <code>IN</code> clause in your query.</p> <p><strong>Pseudo-code:</strong></p> <pre><code>// getting ids from User List&lt;Integer&gt; cityIds = new ArrayList&lt;Integer&gt;(); String query = "select city_ID from user where name = ?"; PreparedStatement ps = con.prepareStatement(query); ps.setString(1, name); ResultSet rs = ps.executeQuery(); while (rs.next()) { cityIds.add(rs.getInt("city_ID")); } // dynamically create your query StringBuilder builder = new StringBuilder("select * from city where ID in ("); for (int i = 0; i &lt; cityIds.size(); i++) { builder.append("?"); if (i &lt; cityIds.size() - 1) { builder.append(","); } } builder.append(")"); // creating PreparedStatement ps = con.prepareStatement(builder.toString()); // adding params to query int index = 1; for (Integer id: cityIds) { ps.setInt(index, id); index++; } ResultSet rs = ps.executeQuery(); while (rs.next()) { // do your stuff } </code></pre> <p><strong>Note:</strong> I recommend you to use parametrized statements which provide much more safer dealing with database, are more human-readable and efficient.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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