Presentation is loading. Please wait.

Presentation is loading. Please wait.

SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Embedded SQL. u Read Sections 8.1, 8.5. Next u Transaction concepts, security u Read Sections 8.6 – 8.7.

Similar presentations


Presentation on theme: "SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Embedded SQL. u Read Sections 8.1, 8.5. Next u Transaction concepts, security u Read Sections 8.6 – 8.7."— Presentation transcript:

1 SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Embedded SQL. u Read Sections 8.1, 8.5. Next u Transaction concepts, security u Read Sections 8.6 – 8.7

2 SCU Fall 2002JoAnne Holliday10–2 Embedding SQL in a Host Language The cursor area is an area for the results of select statements that result in multiple rows. The cursor pointer is a pointer to a row in the collection of rows retrieved by a SQL query. Example: From within a host language, find the names and account numbers of customers with more than the variable amt dollars in some account.

3 SCU Fall 2002JoAnne Holliday10–3 Embedding SQL First, declare a cursor and specify the query in SQL: EXEC SQL declare c cursor for select customer-name, account# from Depositor, Account where Depositor.account#=Account.account# andAccount.balance > :amt END-EXEC This defines the cursor c.

4 SCU Fall 2002JoAnne Holliday10–4 Embedded SQL The query is not done until you do an open, then the query is done and the result is put into the cursor area. (The result may be larger than the actual buffer that is allocated as cursor c. The system takes care of that.) EXEC SQL open c END-EXEC

5 SCU Fall 2002JoAnne Holliday10–5 Embedded SQL You can only look at a single row at a time. This is done with the fetch statement. If cn and an are variables in the host language, this will cause the values of one row in the result to be placed in the variables: EXEC SQL fetch c into :cn :an END-EXEC

6 SCU Fall 2002JoAnne Holliday10–6 Embedded SQL Repeated calls to fetch get successive rows in the query result. The close statement causes the database system to delete the temporary relation that holds the results of the query. EXEC SQL close c END-EXEC

7 SCU Fall 2002JoAnne Holliday10–7 Using Java and JDBC Start with a Connection object, unique to DBMS and installation (see web page). Method createStatement() returns an object of class Statement (if there is no argument) or PreparedStatement if there is an SQL statement as argument.

8 SCU Fall 2002JoAnne Holliday10–8 JDBC Statement stat1 = myCon.createStatement(); PreparedStatement stat2 = myCon.createStatement( "SELECT beer, price " + "FROM Sells" + "WHERE bar = 'Joe''s Bar'" ); myCon is a connection, stat1 is an “empty” statement object, and stat2 is a (prepared) statement object that has an SQL statement associated.

9 SCU Fall 2002JoAnne Holliday10–9 Executing Statements JDBC distinguishes queries (statements that return data) from updates (statements that only affect the database). Methods executeQuery() and executeUpdate() are used to execute these two kinds of SQL statements. u 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.

10 SCU Fall 2002JoAnne Holliday10–10 Example stat1.executeUpdate( "INSERT INTO Sells" + "VALUES('Brass Rail', 'Bud', 3.00)" ); ResultSet Menu = stat2.executeQuery();

11 SCU Fall 2002JoAnne Holliday10–11 Getting the Tuples of a ResultSet Method Next() applies to a ResultSet and moves a “cursor” to the next tuple in that set. u Apply Next() once to get to the first tuple.  Next() returns FALSE if there are no more tuples. While a given tuple is the current of the cursor, you can get its ith 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.

12 SCU Fall 2002JoAnne Holliday10–12 Example while(Menu.Next()) { theBeer = Menu.getString(1); thePrice = Menu.getFloat(2);... }


Download ppt "SCU Fall 2002JoAnne Holliday10–1 Schedule Today u Embedded SQL. u Read Sections 8.1, 8.5. Next u Transaction concepts, security u Read Sections 8.6 – 8.7."

Similar presentations


Ads by Google