I have some duplicated following code
rs = prepStmt.executeQuery();
rs.next();
I want to move it to a method so that I can reuse the code. The method is like:
public static void point(ResultSet rs, PreparedStatement prepStmt){
rs = prepStmt.executeQuery();
result = rs.next();
}
Then I want to get a column value, like, String gender = rs.getString("gender"); And I get the following exception: java.sql.SQLException: Invalid column name gender. The error does not show up when I did't encapsulate the logic in the method. So the column name is correct.
Any ideas? Thanks!
You seem to be assuming that the change to the rs
parameter will be propagated to the calling code. It won't. Everything in Java is passed by value - including references. Instead, you should probably do something like:
public static ResultSet point(PreparedStatement prepStmt) {
ResultSet rs = prepStmt.executeQuery();
result = rs.next();
return rs;
}
You'd then call it as:
ResultSet rs = point(prepStmt);
(It's not clear what result
is either, or whether it's really worth declaring a whole extra method just to avoid calling rs.next()
directly... especially when it means the result of rs.next()
is somewhat obscured...)
See more on this question at Stackoverflow