SQLJ application example in DB2

Source: Internet
Author: User
Tags db2
// This example shows how to use the JDBC application driver to access the SQLJ application of the DB2 database.
// The bank is the database in DB2, and the customer is the table in the bank.
Import java. SQL .*;
Import sqlj. runtime .*;
Import sqlj. runtime. ref .*;
# SQL iterator App_Cursor1 (String customer_id, String customer_name );
# SQL iterator App_Cursor2 (String );
Class App
// Register the driver with DriverManager
Class. forName ("COM. ibm. db2.jdbc. app. DB2Driver"). newInstance ();
Catch (Exception e)
E. printStackTrace ();
Public static void main (String argv [])
App_Cursor1 cursor1;
App_Cursor2 cursor2;
String str1 = null;
String str2 = null;
Int count1;
Connection con = null;
// URL format: jdbc: db2: database name
String url = "jdbc: db2: bank ";
DefaultContext ctx = DefaultContext. Getdefacontext context ();
If (ctx = null ){
Try {
If (argv. length = 0 ){
// Connect with the default user name and password
Con = DriverManager. getConnection (url );
Else if (argv. length = 2 ){
String userid = argv [0];
String passwd = argv [1];
// Connect with the user name and password provided by the user
Con = DriverManager. getConnection (url, userid, passwd );
Else {
System. out. println ("usage: java SqljApp [user name password]");
System. exit (0 );
Con. setAutoCommit (false );
Ctx = new DefaultContext (con );
Catch (SQLException e ){
System. out. println ("Error: cannot get default content ");
System. err. println (e );
System. exit (1 );
DefaultContext. Setdefacontext context (ctx );
// Extract data from the database
System. out. println ("extract some data from the database ...");
# SQL cursor1 = {SELECT customer_id, customer_name from customer };
// Display the result set
// Cursor1.next () returns false if no more rows exist.
System. out. println ("result :");
While (cursor1.next ()){
Str1 = cursor1.customer _ id ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.