(Chapter 9) SQLProgramming
(Chapter 9) SQLProgramming
We have seen how SQL is used at the generic query interface --- an
with SQL.
Want to consider:
Web servers --- Connect users to the database, usually over the
Internet, or possibly a local connection.
2.
3.
Database servers --- Run the DBMS and execute queries and
modifications at the request of the application servers.
checkout?
Answer: Show the how will you pay for this? screen.
For this section, we will deal with the interaction between the
application and the DBMS
A SQP environment is the framework under which data exists and SQL
Environment
Connection
Statement
1.
2.
3.
straightforward.
Java + JDBC
C + CLI
Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);
dynamically loads a driver class for SQL Server db.
3. Establish a connection to the database.
The driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); For SQL
Server.
Connection myCon =
(Others exist)
DriverManager.getConnection(<URL, name, passwd, etc>);
Get an object of class Connection,
which weve called myCon
1.
2.
PreparedStatements.
Statement stat1 = myCon.createStatement();
PreparedStatement stat2 =
myCon.createStatement(
updates.
Statement and PreparedStatement each have methods executeQuery and
executeUpdate.
stat1 is a Statement.
We can use it to insert a tuple as:
stat1.executeUpdate(
INSERT INTO Sells +
VALUES(Brass Rail,Export,3.00)
);
later).
Aside: A cursor is essentially a tuple-variable that ranges over all
satisfying a query.
Method next() advances the cursor to the next tuple.
FROM Sells
WHERE bar = Joes Bar .
Access beer and price from each tuple by:
while ( menu.next() ) {
theBeer = Menu.getString(1);
thePrice = Menu.getFloat(2);
/* do something with theBeer and thePrice */
}
FROM Sells
WHERE bar = Joes Bar .
Access beer and price from each tuple by:
while ( menu.next() ) {
theBeer = Menu.getString(beer);
thePrice = Menu.getFloat(price);
/* do something with theBeer and thePrice */
}
2.
3.
4.
handle H to be executed.
When the SQL statement executed is a query, we need to fetch the tuples
of the result.
SQLFetch(H) gets the next tuple from the result of the statement with
handle H.
SQLBindCol(myStat, 1, , &theBeer, , );
SQLBindCol(myStat, 2, , &thePrice, , );
To connect SQL and the host-language program, the two parts must
function that given a beer and a bar, looks up the price of that beer at
that bar.
Assumes database has our usual Sells(bar, beer, price) relation.
Macro NOT FOUND is true if and only if the FETCH fails to find a
tuple.
EXEC SQL
DECLARE c CURSOR FOR
SELECT depositor.customer_name, customer_city
FROM depositor, customer, account
WHERE depositor.customer_name = customer.customer_name
AND depositor account_number = account.account_number
AND account.balance > :amount ;
Can update tuples fetched by cursor by declaring that the cursor is for
update
DECLARE c CURSOR FOR
SELECT *
FROM account
WHERE branch_name = Perryridge
FOR UPDATE
To update tuple at the current location of cursor c
UPDATE account
SET balance = balance + 100
WHERE CURRENT OF c
Write C + SQL to print Joes menu the list of beer-price pairs that we
run time.
Preparing a query:
q is an SQL variable
representing the
optimized form of
whatever statement
is typed into :query
If we are only going to execute the query once, we can combine the