EasyManuals Logo

IBM DB2 User Manual

IBM DB2
585 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #23 background imageLoading...
Page #23 background image
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

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the IBM DB2 and is the answer not in the manual?

IBM DB2 Specifications

General IconGeneral
DeveloperIBM
Initial Release1983
LicenseProprietary
Written inC, C++
CategoryDatabase Management System (RDBMS)
Operating SystemLinux, Windows
Programming LanguagesC, C++
Data ModelsRelational
EditionsCommunity, Standard, Advanced
Key Featureshigh availability, scalability