O'Reilly logo

MySQL™ and JSP™ Web Applications: Data-Driven Programming Using Tomcat and MySQL by James Turner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Working with ResultSets

When you get a ResultSet back from a query, there are several ways to get at the data inside. You can refer to the columns positionally or by name. Let's look at a few code fragments to demonstrate the difference:

ResultSet rs = st.executeQuery("SELECT * FROM employees");

while (rs.next()) {
   String lname = rs.getString("lname_txt");
}

This example uses reference by name. You ask for column values by using the name of the column as an argument to the getX method:

ResultSet rs = st.executeQuery("SELECT lname_txt, fname_txt FROM employees");

while (rs.next()) {
   String lname = rs.getString(1);
}

This uses positional reference. The integer argument to getString refers to the position of the column in the list of columns ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required