//***************************************************************************
// (c) Copyright IBM Corp. 2007 All rights reserved.
//
// The following sample of source code ("Sample") is owned by International
// Business Machines Corporation or one of its subsidiaries ("IBM") and is
// copyrighted and licensed, not sold. You may use, copy, modify, and
// distribute the Sample in any form without payment to IBM, for the purpose of
// assisting you in the development of your applications.
//
// The Sample code is provided to you on an "AS IS" basis, without warranty of
// any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR
// IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do
// not allow for the exclusion or limitation of implied warranties, so the above
// limitations or exclusions may not apply to you. IBM shall not be liable for
// any damages you suffer as a result of using, copying, modifying or
// distributing the Sample, even if IBM has been advised of the possibility of
// such damages.
//***************************************************************************
//
// SOURCE FILE NAME: DbUse.sqlj
//
// SAMPLE: How to use a database
//
// SQL Statements USED:
// CREATE TABLE
// DROP TABLE
// DELETE
// COMMIT
// ROLLBACK
//
// Classes used from Util.sqlj are:
// Db
//
//
// Output will vary depending on the JDBC driver connectivity used.
//***************************************************************************
//
// For more information on the sample programs, see the README file.
//
// For information on developing Java applications see the Developing Java Applications book.
//
// For information on using SQL statements, see the SQL Reference.
//
// For the latest information on programming, compiling, and running DB2
// applications, visit the DB2 Information Center at
// http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp
//**************************************************************************/
import java.sql.*;
import sqlj.runtime.*;
import sqlj.runtime.ref.*;
class DbUse
{
public static void main(String argv[])
{
try
{
Db db = new Db(argv);
System.out.println();
System.out.println("THIS SAMPLE SHOWS HOW TO USE A DATABASE.");
// connect to the 'sample' database
db.getDefaultContext();
StaticStmtInvoke();
StaticStmtInvokeWithHostVars();
// disconnect from the 'sample' database
db.disconnect();
}
catch (Exception e)
{
System.out.println(e);
}
} // main
static void StaticStmtInvoke()
{
try
{
System.out.println();
System.out.println(
"----------------------------------------------------------\n" +
"USE THE SQL STATEMENTS:\n" +
" CREATE TABLE\n" +
" DROP TABLE\n" +
"TO SHOW HOW TO INVOKE SQL STATEMENTS.");
// create a table
System.out.println();
System.out.println(
" Invoke the statement:\n" +
" CREATE TABLE table1(col1 INTEGER)");
#sql {CREATE TABLE table1(col1 INTEGER)};
System.out.println();
System.out.println(" COMMIT");
#sql {COMMIT};
// drop a table
System.out.println();
System.out.println(" Invoke the statement:\n" +
" DROP TABLE table1");
#sql {DROP TABLE table1};
System.out.println();
System.out.println(" COMMIT");
#sql {COMMIT};
}
catch (Exception e)
{
System.out.println(e);
}
} // StaticStmtInvoke
static void StaticStmtInvokeWithHostVars()
{
try
{
int hostVar1;
String hostVar2;
System.out.println();
System.out.println(
"----------------------------------------------------------\n" +
"USE THE SQL STATEMENTS:\n" +
" DELETE\n" +
" ROLLBACK\n" +
"TO SHOW HOW TO USE HOST VARIABLES.");
// execute a statement with host variables
System.out.println();
System.out.println(
" Execute:\n" +
" DELETE FROM org\n" +
" WHERE deptnumb = :hostVar1\n" +
" AND division = :hostVar2\n" +
" for\n" +
" hostVar1 = 15\n" +
" hostVar2 = 'Eastern'");
hostVar1 = 15;
hostVar2 = "Eastern";
#sql {DELETE FROM org
WHERE deptnumb = :hostVar1 AND
division = :hostVar2};
// rollback the transaction
System.out.println();
System.out.println(" Rollback the transaction.");
#sql {ROLLBACK};
}
catch (Exception e)
{
System.out.println(e);
}
} // StaticStmtInvokeWithHostVars
} // DbUse