//***************************************************************************
// (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: DtUdt.sqlj
//
// SAMPLE: How to create, use and drop user defined distinct types
//
// SQL statements USED:
// CREATE DISTINCT TYPE
// CREATE TABLE
// DROP DISTINCT TYPE
// DROP TABLE
// INSERT
// COMMIT
//
// Classes used from Util.sqlj are:
// Db
// SqljException
//
//
// 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.lang.*;
import java.sql.*;
import sqlj.runtime.*;
import sqlj.runtime.ref.*;
class DtUdt
{
public static void main(String argv[])
{
DefaultContext ctx = null;
try
{
Db db = new Db(argv);
System.out.println();
System.out.println("THIS SAMPLE SHOWS HOW TO CREATE, USE AND DROP\n" +
"USER DEFINED DISTINCT TYPES.");
// connect to the 'sample' database
ctx = db.getDefaultContext();
create();
use( ctx.getConnection() );
drop();
// disconnect from the 'sample' database
db.disconnect();
}
catch (Exception e)
{
SqljException sqljExc = new SqljException(e);
sqljExc.handle();
}
} // main
// This function creates a few user defined distinct types
static void create()
{
try
{
System.out.println();
System.out.println(
"----------------------------------------------------------\n" +
"USE THE SQL STATEMENTS:\n" +
" CREATE DISTINCT TYPE\n" +
" COMMIT\n" +
"TO CREATE UDTs.");
System.out.println();
System.out.println(
" CREATE DISTINCT TYPE udt1 AS INTEGER WITH COMPARISONS");
#sql {CREATE DISTINCT TYPE udt1 AS INTEGER WITH COMPARISONS};
System.out.println(
" CREATE DISTINCT TYPE udt2 AS CHAR(2) WITH COMPARISONS");
#sql {CREATE DISTINCT TYPE udt2 AS CHAR(2) WITH COMPARISONS};
System.out.println(
" CREATE DISTINCT TYPE udt3 AS DECIMAL(7, 2) WITH COMPARISONS");
#sql {CREATE DISTINCT TYPE udt3 AS DECIMAL(7, 2) WITH COMPARISONS};
System.out.println(" COMMIT");
#sql {COMMIT};
}
catch (Exception e)
{
SqljException sqljExc = new SqljException(e);
sqljExc.handle();
}
} // create
// This function uses the user defined distinct types that we created
// at the beginning of this program.
static void use(Connection con)
{
System.out.println();
System.out.println(
"----------------------------------------------------------\n" +
"USE THE SQL STATEMENTS:\n" +
" EXECUTE IMMEDIATE\n" +
" COMMIT\n" +
"TO USE UTDs.");
// create udt_table
try
{
String strStmt;
System.out.println();
System.out.println(
" CREATE TABLE udt_table(col1 udt1, col2 udt2, col3 udt3)");
strStmt = "CREATE TABLE udt_table(col1 udt1, col2 udt2, col3 udt3)";
Statement stmt = con.createStatement();
stmt.execute(strStmt);
System.out.println(" COMMIT");
#sql {COMMIT};
}
catch (Exception e)
{
SqljException sqljExc = new SqljException(e);
sqljExc.handle();
}
// insert udt_table
try
{
String strStmt;
System.out.println();
System.out.println(
" INSERT INTO udt_table \n" +
" VALUES(CAST(77 AS udt1),\n" +
" CAST('ab' AS udt2),\n" +
" CAST(111.77 AS udt3))");
strStmt = "INSERT INTO udt_table VALUES (CAST(77 AS udt1), " +
" CAST('ab' AS udt2), " +
" CAST(111.77 AS udt3))";
Statement stmt = con.createStatement();
stmt.execute(strStmt);
System.out.println(" COMMIT");
#sql {COMMIT};
}
catch (Exception e)
{
SqljException sqljExc = new SqljException(e);
sqljExc.handle();
}
// drop udt_table
try
{
String strStmt;
System.out.println();
System.out.println(" DROP TABLE udt_table");
strStmt = "DROP TABLE udt_table ";
Statement stmt = con.createStatement();
stmt.execute(strStmt);
System.out.println(" COMMIT");
#sql {COMMIT};
}
catch (Exception e)
{
SqljException sqljExc = new SqljException(e);
sqljExc.handle();
}
} // use
// This function drops all of the user defined distinct types that
// we created at the beginning of this program
static void drop()
{
System.out.println();
System.out.println(
"----------------------------------------------------------\n" +
"USE THE SQL STATEMENTS:\n" +
" DROP\n" +
" COMMIT\n" +
"TO DROP UDTs.");
try
{
System.out.println();
System.out.println(" DROP USER DISTINCT TYPE udt1");
#sql {DROP DISTINCT TYPE udt1};
System.out.println(" DROP USER DISTINCT TYPE udt2");
#sql {DROP DISTINCT TYPE udt2};
System.out.println(" DROP USER DISTINCT TYPE udt3");
#sql {DROP DISTINCT TYPE udt3};
System.out.println(" COMMIT");
#sql {COMMIT};
}
catch (Exception e)
{
SqljException sqljExc = new SqljException(e);
sqljExc.handle();
}
} // drop
} // DtUdt