1

I have two tables the first one is for "course" and the other is for "exam":

enter image description here enter image description here

I want select exams which are matched to the code, of course, get by entering its name (nomCours)

here is my code :

try {
    String select1 = "select code from cours where nomCours=?";
    st2 = con.prepareStatement(select1);
    st2.setString(1, nom);
    rs2 = st2.executeQuery();
    System.out.println(rs2.getInt("code"));

    String select = "SELECT matricule FROM examen where code=?";
    st = con.prepareStatement(select);
    st.setInt(1, rs2.getInt("code"));
    rs = st.executeQuery();
    while (rs.next()) {
        System.out.print(rs.getInt("matricule")+"  ");
    }
} catch (SQLException ex) {
    System.err.print(ex);
}

this doesn't work, and it gives me this error :

java.sql.SQLException: Before start of result set
0

1 Answer 1

4

When you execute a query, you get a ResultSet that points to the so-called before-first row. You need to advance it in order to get the first row of the result:

rs2 = st2.executeQuery();
if (rs2.next()) {
    System.out.println(rs2.getInt("code"));
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.