














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The JAVA Way, JDBC and SQLJ, Access RDBMS, ODBC, Embedded SQL, SQLJ, JAVA API, JDBC Drivers, JAVA Exception Handling, JDBC Query, JDBC Update, Transactions, Database Connection, SQLJ, SQLJ Update, Iterators, SQL in JAVA
Typology: Slides
1 / 22
This page cannot be seen from the preview
Don't miss anything!















School of Computer Science University of Waterloo
http://www.software.ibm.com/data/db2/java/
// DB2 UDB JDBC Samples // // (c) Copyright International Business Machines // Corporation, 1996, 1997. All Rights Reserved.
import java.sql.*;
class Appl {
static { try { // register the driver with DriverManager Class.forName("COM.ibm.db2.jdbc.app.DB2Driver"); } catch (Exception e) { e.printStackTrace(); } }
// get a statement handle Statement stmt = con.createStatement();
// retrieve data from the database ResultSet rs = stmt.executeQuery("SELECT (^) * from publication");
System.out.println("Received results:");
// display the result set // rs.next() returns false when there are no more rows while (rs.next()) { String a = rs.getString(1); String str = rs.getString(2);
System.out.print(" pubid= " + a); System.out.print(" title= " + str); System.out.print("\n"); }
rs.close(); stmt.close();
// DB2 UDB SQLJ Samples // // (c) Copyright International Business Machines // Corporation, 1998. All Rights Reserved.
import java.sql.; import sqlj.runtime.;
#sql iterator App_Cursor1 (String empno, String firstnme) ; #sql iterator App_Cursor2 (String) ;
class App { static { try { // register the driver with DriverManager Class.forName("COM.ibm.db2.jdbc.app.DB2Driver"); } catch (Exception e) { e.printStackTrace(); } }
public static void main(String argv[]) {
try {
Connection con = null;
// URL is jdbc:db2:dbname String url = "jdbc:db2:cs448";
DefaultContext ctx = DefaultContext.getDefaultContext(); if (ctx == null) { try { // connect with default id/password con = DriverManager.getConnection(url); ctx = new DefaultContext(con); } catch (SQLException e) { System.out.println("Error: no default context"); System.err.println(e) ; System.exit(1); }
DefaultContext.setDefaultContext(ctx); }
try { // update the database #sql { UPDATE author set name = :string where aid = :aid };
// rollback the update #sql { ROLLBACK work }; } catch( Exception e ) { e.printStackTrace(); }