views:

69

answers:

1

Haven't found in docs.

Does java ResultSet supports query arguments,like jdbcTemplate?

For example, something like:

int length = 10;
ResultSet rs = stmt.executeQuery("select MyTable.COLOR from MyTable where MyTable.LENGTH = ?", new Object[] { length });

is it possible?

Thank you.

+4  A: 

PreparedStatement allows (numbered) parameters:

PreparedStatement pstmt = connection.prepareStatement(
      "select MyTable.COLOR from MyTable where MyTable.LENGTH=?");

pstmt.setInt(1, desiredLength);

ResultSet rs = pstmt.executeQuery();
Bozho
Here's a tutorial: http://download.oracle.com/javase/tutorial/jdbc/basics/prepared.html
BalusC
Hi,Bozho. I'm interested in returned value - that will be multiple rows from DB table, selected by one ID param.
sergionni
Just iterate through the `ResultSet` the usual way using `ResultSet#next()` in a `while` loop. That's also covered in one of the chapters of the aforementioned tutorial.
BalusC
And this is the goal of my question- how to get ResultSet with argument ID in query. as i understand, this way isn't possible:ResultSet rs = stmt.executeQuery("select MyTable.COLOR from MyTable where MyTable.id = ?", new Object[] { id });
sergionni
Did you read Bozho's answer and the tutorial link?
BalusC
yes, now is clear, thank you BalusC
sergionni