Java Reference
In-Depth Information
What is the JDBC?
The JDBC (short for Java Database Connectivity) interface is a pure Java API used to execute
SQL statements. The JDBC provides a set of classes and interfaces that can be used by devel-
opers to write database applications. Basic JDBC interaction, in its simplest form, can be bro-
ken down into four steps:
1.
Open a connection to the database.
2.
Execute a SQL statement.
3.
Process the results.
4.
Close the connection to the database.
The following code fragment shows these steps in action:
// Step 1. Open a connection to the ODBC datasource titles.
con = DriverManager.getConnection(“jdbc:odbc:titles”,
“username”, “password”);
// Step 2. Execute the SQL statement.
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery(“SELECT * “ +
“FROM Types”);
// Step 3. Process the Results
while ( rs.next() ) {
// get the type_id, which is an int
System.err.println(“Type ID = “ + rs.getInt(“type_id”));
// get the type_name, which is a String
System.err.println(“Type Name = “ + rs.getString(“type_name”));
}
// Step 4. Close the Connection.
rs.close();
con.close();
This chapter will introduce the JDBC and explore how to use it in a servlet.
Two- and Three-Tier Database Access Models
The JDBC provides support for two- and three-tier database access models. We will examine
both in this section.
Search WWH ::




Custom Search