Download presentation
Presentation is loading. Please wait.
1
Client Access, Queries, Stored Procedures, JDBC
CS 433 DB2 Overview Client Access, Queries, Stored Procedures, JDBC 9/12/01 Jeff Derstadt & Megha Batra
2
Administration Project Proposals Due 09/14/01 by 5PM Email them to TAs
Please register your group on the web
3
Client Configuration Must setup a client-server connection
Server name: egret.csuglab.cornell.edu Port: 50000 Database name: Your group (g7, g23, etc.)
4
Client Configuration Let’s configure our client!
5
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>
6
Command Line Queries Let’s issue some queries!
7
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 her requesting that she sign up for additional instruction Converting relational tables to Xml
8
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)
9
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
10
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
11
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
12
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
13
Stored Procedures Let’s write a stored procedure that converts a table to Xml!
14
Questions ??
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.