Chapter 3. JDBC application programming
Writing a JDBC application has much in common with writing an SQL application
in any other language.
In general, you need to do the following things:
v Access the Java packages that contain JDBC methods.
v Declare variables for sending data to or retrieving data from DB2 tables.
v Connect to a data source.
v Execute SQL statements.
v Handle SQL errors and warnings.
v Disconnect from the data source.
Although the tasks that you need to perform are similar to those in other
languages, the way that you execute those tasks is somewhat different.
Example of a simple JDBC application
A simple JDBC application demonstrates the basic elements that JDBC applications
need to include.
import java.sql.*; 1
public class EzJava
{
public static void main(String[] args)
{
String urlPrefix = "jdbc:db2:";
String url;
String empNo; 2
Connection con;
Statement stmt;
ResultSet rs;
System.out.println ("**** Enter class EzJava");
// Check the that first argument has the correct form for the portion
// of the URL that follows jdbc:db2:,
// as described
// in the Connecting to a data source using the DriverManager
// interface with the IBM Data Server Driver for JDBC and SQLJ topic.
// For example, for IBM Data Server Driver for
// JDBC and SQLJ type 2 connectivity,
// args[0] might be MVS1DB2M. For
// type 4 connectivity, args[0] might
// be //stlmvs1:10110/MVS1DB2M.
if (args.length==0)
{
System.err.println ("Invalid value. First argument appended to "+
"jdbc:db2: must specify a valid URL.");
System.exit(1);
}
url = urlPrefix + args[0];
Figure 1. Simple JDBC application
© Copyright IBM Corp. 1998, 2008 7