//***************************************************************************
// (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: Applt.sqlj
//
// SAMPLE: An SQLJ applet that uses a JDBC applet driver to access a database
//
// This sample shows how to write an SQLJ applet that uses the
// JDBC Type 4 driver to access a DB2 database.
//
// This sample uses JDBC Type 4 driver to connect to
// the "sample" database. Run this sample using the
// following steps:
// 1. Create and populate the "sample" database with the following
// command: db2sampl
//
// 2. Customize Applt.html with your server, port, user ID, and
// password. Refer to Applt.html for details.
//
// 3. Compile the program with the following command (you have to
// hardcode the userid and password in the build file, bldsqlj,
// and update the port number if needed):
// bldsqlj Applt
//
// Alternatively, you can compile the program with the following
// command if you have a compatible make/nmake program on
// your system:
// make/nmake Applt
//
// 4. Ensure that your working directory is accessible by your web
// browser. If it is not, copy Applt.class and Applt.html into
// a directory that is accessible.
//
// 5. To use the JDBC Type 4 driver, copy sqllib\java\db2jcc.jar on
// Windows or sqllib/java/db2jcc.jar on UNIX, into the same
// directory as Applt.class and Applt.html.
//
// 6. To run this sample, start your web browser (which must support
// Java 1.3) and load Applt.html on your client machine.
// You can view it locally with the following command:
// appletviewer Applt.html
//
//
// SQL Statements USED:
// SELECT
// UPDATE
// ROLLBACK
//
// None
//***************************************************************************
//
// 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 java.awt.*;
import java.applet.Applet;
import sqlj.runtime.*;
import sqlj.runtime.ref.*;
#sql iterator Applt_Cursor1(String empno, String firstnme);
#sql iterator Applt_Cursor2(String);
public class Applt extends Applet
{
Connection con;
public void init()
{
try
{
DefaultContext ctx = DefaultContext.getDefaultContext();
if (ctx == null)
{
// get parameter values from the html page
String server = getParameter("server");
String port = getParameter("port");
// construct the URL (sample is the database name)
String url = "jdbc:db2://"+server+":"+port+"/sample";
String userid = getParameter("userid");
String password = getParameter("password");
String driverType = getParameter("driverType");
// use driverType=4
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
// connect to the 'sample' database with user ID and password
con = DriverManager.getConnection(url, userid, password);
con.setAutoCommit(false);
ctx = new DefaultContext(con);
DefaultContext.setDefaultContext(ctx);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void paint(Graphics g)
{
try
{
Applt_Cursor1 cursor1;
Applt_Cursor2 cursor2;
String str1 = null;
String str2 = null;
long count1;
// retrieve data from database
g.drawString(
"First, let's retrieve some data from the database...", 10, 10);
#sql cursor1 = {SELECT empno, firstnme FROM employee};
g.drawString("Received results:", 10, 25);
// display the result set
// cursor1.next() returns false when there are no more rows
int y = 50;
int i = 0;
while (cursor1.next() && (i < 2))
{
i++;
str1 = cursor1.empno();
str2 = cursor1.firstnme();
String oneLine = " empno= " + str1 + " firstname= " + str2;
g.drawString(oneLine, 20, y);
y = y + 15;
}
cursor1.close();
// retrieve data from the database
y = y + 40;
g.drawString(
"Retrieve the number of rows in employee table...", 10, y);
#sql {SELECT count(*) INTO :count1 FROM employee};
y = y + 15;
if (1 == count1)
{
g.drawString(
"There is " + count1 + " row in employee table.", 10, y);
}
else
{
g.drawString(
"There are " + count1 + " rows in employee table.", 10, y);
}
// update the database
y = y + 40;
g.drawString("Now, update the database...", 10, y);
#sql {UPDATE employee set firstnme = 'SHILI' where empno = '000010'};
// retrieve the updated data from the database
y = y + 40;
g.drawString(
"Retrieve the updated data from the database...", 10, y);
str1 = "000010";
#sql cursor2 = {SELECT firstnme from employee where empno = :str1};
// display the result set
// cursor2.next() returns false when there are no more rows
y = y + 15;
g.drawString("Received results:", 10, y);
y = y + 25;
while (true)
{
#sql {FETCH :cursor2 INTO :str2};
if (cursor2.endFetch()) break;
String oneLine = " empno= " + str1 + " firstname= " + str2;
g.drawString(oneLine, 20, y);
y = y + 15;
}
cursor2.close();
// roll back the update
y = y + 40;
g.drawString("Now, roll back the update...", 10, y);
#sql {ROLLBACK work};
y = y + 15;
g.drawString("Rollback done.", 10, y);
}
catch (Exception e)
{
e.printStackTrace();
}
}
} // Applt