Client Access, Queries, Stored Procedures, JDBC CS 433 DB2 Overview Client Access, Queries, Stored Procedures, JDBC 9/12/01 Jeff Derstadt & Megha Batra
Administration Project Proposals Due 09/14/01 by 5PM Email them to TAs Please register your group on the web http://www.cs.cornell.edu/courses/cs433/2001fa/
Client Configuration Must setup a client-server connection Server name: egret.csuglab.cornell.edu Port: 50000 Database name: Your group (g7, g23, etc.)
Client Configuration Let’s configure our client!
Command Line Queries Connect to database Issue queries Connect to <database_name> user <user_name> using <password> Issue queries CREATE TABLE <table> (<column_name> <datatype>, …) INSERT INTO <table> values (…) SELECT <columns> from <table>
Command Line Queries Let’s issue some queries!
Stored Procedures Definition: Examples: A set of SQL and programming language (C++/Java) statements that are compiled into a named database object that can be invoked by a user application Examples: For each sailor with a rating less than 5, insert her into the “NeedsTraining” table, and email her requesting that she sign up for additional instruction Converting relational tables to Xml
Stored Procedures (2) Advantages Can embed SQL in a ‘native’ programming language Executed on the database server: no network delay Compiled procedures can be invoked by any user application Provides consistent functionality (don’t need to rewrite SQL ‘inserts’, can just call a stored procedure)
Java DB2 allows you to write stored procedures in Java + Java is an easy language - Must use some sort of bridge between the native DB2 code and the Java Virtual Machine to transfer data from a relational table to the stored procedure +/- JDBC provides this bridge: extra copy, but easy access
DB2 to Java 3 1 2 Java Virtual Machine Int sid = rs.getInt(1); String name = rs.getString(2); 3 ResultSet rs; Result extracted from JDBC DB2 (C++) SID=5, NAME=Sarah Query Result 1 JDBC (Java Database Connection) 2 Copy from DB2 to JDBC storage
JDBC java.sql.* Using JDBC adds additional costs However Copy from DB2 to JDBC ResultSet object (2) Copy from ResultSet object to another Java variable (sometimes at 3) However JDBC makes issuing SQL queries and gathering results very easy
JDBC ResultSet Provides a simple cursor object A cursor is a set-of-records abstraction that allows you to move from one row to the next and access the columns: SID NAME rs.next() rs.getString(2) 12 Sarah 13 Joe 15 Megha
Stored Procedures Let’s write a stored procedure that converts a table to Xml!
Questions ??