JDBC error with PreparedStatement
<p>I'm trying to write a small function that gets an id u (integer), and returns the number of friends u have with distance &lt;=3 (the friends information is stored in the table <strong>Likes</strong>: Likes(uid1, uid2) means: uid1 likes uid2).<br>
I wrote this simple query:  </p>

<pre><code>   String likesDistance = 
        "SELECT uid2 " + //DISTANCE = 1
        "FROM Likes " +
        "WHERE uid1 = ? " +
        "UNION " +
        "SELECT L2.uid2 " + //DISTANCE = 2
        "FROM Likes L1, Likes L2 " +
        "WHERE L1.uid1 = ? and L1.uid2 = L2.uid1 " +
        "UNION "+
        "SELECT L3.uid2 " + //DISTANCE = 3
        "FROM Likes L1, Likes L2 , Likes L3 " +
        "WHERE L1.uid1 = ? and L1.uid2 = L2.uid1 and L2.uid2 = L3.uid1 ";
</code></pre>

<p>Then, I do the following:  </p>

<pre><code> PreparedStatement pstmt2 = con.prepareStatement(likesDistance);  
        pstmt1.setInt(1, u);  
        pstmt1.setInt(2, u);  
        System.out.println("2");  
        pstmt1.setInt(3, u);  
        System.out.println("3");  
        pstmt1.setInt(4, u);  
        pstmt1.setInt(5, u);  
</code></pre>

<p>Where u, as mention before, is an integer passed to the function.<br>
All this code is within a 'try' block. When I try to run it, it prints the first printout ("2"), but not the second one ("3"), and I get the following exception message:<br>
<strong>The column index is out of range: 3, number of columns: 2.</strong>  </p>

<p>Why is it like this, and how can I change it to work as I want?  </p>

<p>Thanks a lot.</p>
