How can I get a value from a ResultSet by the attribute name?
DB-Table (table):
name:TEXT | age:INT | gender:INT
SQL-Query:
SELECT * FROM table WHERE name='john'
The output is in a java.sql.ResultSet
How can I get a variable by column's name (name, age, gender)?
You can itterate over the values like this:
Statement stmt = null;
String query = "SELECT * FROM table WHERE name='john'";
try {
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String name= rs.getString("name");
int age= rs.getInt("age");
int gender = rs.getInt("gender");
}
} catch (Exception e ) {
e.printStackTrace();
} finally {
if (stmt != null) { stmt.close(); }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With