








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









Winter 2002
Arthur Keller – CS 180
Winter 2002
Arthur Keller – CS 180
Winter 2002
Arthur Keller – CS 180
Winter 2002
Arthur Keller – CS 180 Cursors
Similar to PL/SQL cursors, with some syntactic differences.ExamplePrint Joe’s menu.
Sells(bar, beer, price)EXEC SQL BEGIN DECLARE SECTION;
char
theBeer[21]; float
thePrice;
EXEC SQL END DECLARE SECTION;EXEC SQL DECLARE c CURSOR FOR
SELECT beer, priceFROM SellsWHERE bar =
'Joe''s
Bar';
EXEC SQL OPEN CURSOR c;while(1) {
EXEC SQL FETCH c
INTO :theBeer, :thePrice; if(NOT FOUND) break; /* format and print beer and price */} EXEC SQL CLOSE CURSOR c;
Winter 2002
Arthur Keller – CS 180
Winter 2002
Arthur Keller – CS 180
char
query[MAX_QUERY_LENGTH];
issue
prompt
read
user's
text
into
array
query
q^
:query;
q;
go^
back
to
reissue
prompt
-^ Once prepared, a query can be executed many times.
◆^ “Prepare” = optimize the query,
e.g ., find a way to execute it using
few disk-page I/O’s.
-^ Alternatively,
:query;
Winter 2002
Arthur Keller – CS 180
-^ In C, library calls let you create a
See text. See also Monjian book for PostgreSQL.
-^ Use
-^ Use
SQLPrepare(handle1,~"SELECT~beer,~price
Sells WHERE
bar
'Joe''s
Bar'");
SQLExecute(handle1);
Winter 2002
Arthur Keller – CS 180
applies to a handle, column number, and variable, plus
other arguments (see text).
applies to a handle.
SQLBindCol(handle1,
&theBar,...)
SQLBindCol(handle1,
&thePrice,...)
SQLExecute(handle1);
while(SQLFetch(handle1)
Winter 2002
Arthur Keller – CS 180
-^ JDBC distinguishes queries (statements that return data) from^ updates
-^ Methods
They must have an argument if applied to a
Statement
, never if applied
to a^
PreparedStatement
-^ When a query is executed, it returns an object of class^ ResultSet
stat1.executeUpdate("INSERT
Sells"
"VALUES('Brass
Rail',
'Bud',
);ResultSet
Menu
stat2.executeQuery();
Winter 2002
Arthur Keller – CS 180
Getting the Tuples of a
ResultSet
-^ Method
Next
() applies to a
ResultSet
and moves a
“cursor” to the next tuple in that set.^ ◆^
-^ While a given tuple is the current of the cursor, you canget its
i th component by applying to a
ResultSet
a method
of the form
get
X
(i),
where
X^
is the name for the
type of that component.Example^ while(Menu.Next())
theBeer
Menu.getString(1);
thePrice
Menu.getFloat(2); ...
}