Hello guys, I am very new to Java. I've been trying to select two different tables but fails. The error is "Invalid operation at current cursor position.". Here is my code.

private void display() throws SQLException{
        
        TableModel model = schedTable.getModel();
        
        PreparedStatement stmt = con.prepareStatement("select * from APP.SCHEDULE");
        ResultSet sched = stmt.executeQuery();
        
        while(sched.next()){
            stmt = con.prepareStatement("select * from APP.SUBJECT where subjectNum='OOP2'");
            ResultSet subj = stmt.executeQuery();
                     
            model.setValueAt(sched.getInt("numOfStud"), 0, 0);
            
            model.setValueAt(subj.getString("name"), 0, 1);
            
            break;
        }
    }

As you can see, I am still learning this stuff. I haven't fully understood how statement and resultset works yet. I would very much appreciate it if you would explain it to me.

solved it already. All I needed was a subj.next(). dang Im so dumb. Took me 3 hours to solve that.

You better have a look at your line 14. Here you are trying to access

subj.getString("name")

where as you have not checked whether subj has a result from executing the query.
Since you are using a conditionalized SQL you can use in line 14 like

if(subj.next()){
  model.setValueAt(subj.getString("name"), 0, 1);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.