View RSS Feed


JDBC: Query the Database

Rate this Entry
by , 04-27-2012 at 08:03 PM (862 Views)
Querying a database means searching through its data. You do so be sending SQL statements to the database. Once you have an open connection, you need to create a Statement object, like this:
Java Code:
Statement statement = connection.createStatement();
Once you have created the Statement you can use it to execute SQL queries, like this:
Java Code:
String sql = "select * from people";

ResultSet result = statement.executeQuery(sql);
When you execute an SQL query you get back a ResultSet. The ResultSet contains the result of your SQL query. The result is returned in rows with columns of data. You iterate the rows of the ResultSet like this:
Java Code:
while( {

    String name = result.getString("name");
    long   age  = result.getLong  ("age");


Submit "JDBC: Query the Database" to Facebook Submit "JDBC: Query the Database" to Digg Submit "JDBC: Query the Database" to Submit "JDBC: Query the Database" to StumbleUpon Submit "JDBC: Query the Database" to Google