DB2 Overview
Download
Report
Transcript DB2 Overview
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
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:
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
Java Virtual Machine
Int sid = rs.getInt(1);
DB2
String name = rs.getString(2);
(C++)
Query
Result
1
Copy from DB2 to
JDBC storage
SID=5, NAME=Sarah
2
Result extracted
from JDBC
3
ResultSet
rs;
JDBC (Java Database Connection)
JDBC java.sql.*
Using JDBC adds additional costs
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:
rs.next()
SID
NAME
12
Sarah
13
Joe
15
Megha
rs.getString(2)
Stored Procedures
Let’s write a stored procedure that
converts a table to Xml!
Questions ??