CS 433 DB2 Overview Client Access, Queries, Stored Procedures ...

380 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
380
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

CS 433 DB2 Overview Client Access, Queries, Stored Procedures ...

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

×