View RSS Feed

JDBC

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(result.next()) {

    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 del.icio.us Submit "JDBC: Query the Database" to StumbleUpon Submit "JDBC: Query the Database" to Google

Categories
Query

Comments