You can retrieve data using the executeQuery method of a PreparedStatement, which queries the database with a user-defined SQL statement. This method returns the query result as a ResultSet. The ResultSet can then be traversed to fetch the queried data.
A ResultSet contains the following methods that allow you to navigate through the query results of a SQL SELECT statement:
next Move to the next row.
previous Move to the previous row.
To SELECT data from a database
Prepare a new SQL statement as a String.
String sql_string = "SELECT * FROM Department ORDER BY dept_no"; |
Assign the String to the PreparedStatement.
PreparedStatement select_statement = conn.prepareStatement(sql_string); |
Execute the statement and assign the query results to a ResultSet.
ResultSet cursor = select_statement.executeQuery(); |
Traverse through the ResultSet and retrieve the data.
// Get the next row stored in the ResultSet. cursor.next(); // Store the data from the first column in the table. int dept_no = cursor.getInt(1); // Store the data from the second column in the table. String dept_name = cursor.getString(2); |
Close the ResultSet to free resources.
cursor.close(); |
Close the PreparedStatement to free resources.
select_statement.close() |
Send feedback about this page via email or DocCommentXchange | Copyright © 2008, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.0 |