February 19, 2008

Basic JDBC: Executing a simple SELECT statement


// Load the JDBC driver.
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");

// Establish a connection
url = "jdbc:db2:INSURANCE";
username = "myName";
password="myPassword";
Connection connection = DriverManager.getConnection(url,username,password);

// Create and execute query
String query = "SELECT name, age, dob FROM COMMERCIAL.AUTO";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
String name = rs.getString("name");
int age = rs.getInt("age");
Date = rs.getDate("dob");
}

connection.close();

No comments: