Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Management Systems 1 Raghu Ramakrishnan Database Application Development Chpt 6 Xin Zhang.

Similar presentations


Presentation on theme: "Database Management Systems 1 Raghu Ramakrishnan Database Application Development Chpt 6 Xin Zhang."— Presentation transcript:

1 Database Management Systems 1 Raghu Ramakrishnan Database Application Development Chpt 6 Xin Zhang

2 Database Management Systems 2 Raghu Ramakrishnan Embedded SQL v SQL commands can be called from within a host language (e.g., C or Java ) program. –SQL statements can refer to host variables (including special variables used to return status). –Must include a statement to connect to the right database. v SQL relations are set-oriented. –SQL supports a mechanism called cursor to handle this.

3 Database Management Systems 3 Raghu Ramakrishnan Cursors v Can declare a cursor on a relation or query statement (which generates a relation). v Can open a cursor, and repeatedly fetch a tuple then move the cursor, until all tuples have been retrieved. –Can use a special clause, called ORDER BY, in queries that are accessed through a cursor, to control the order in which tuples are returned. u Fields in ORDER BY clause must also appear in SELECT clause. –The ORDER BY clause, which orders answer tuples, is only allowed in the context of a cursor. v Can also modify/delete tuple pointed to by a cursor.

4 Database Management Systems 4 Raghu Ramakrishnan Cursor Declaration EXEC SQL DECLARE sinfo CURSOR FOR SELECT S.sname FROM Sailors S WHERE rating >7 ORDER BY S.sname Find names, and ages of those sailors who’s rating is above 7, in alphabetical order

5 Database Management Systems 5 Raghu Ramakrishnan Embedding SQL in C: An Example char SQLSTATE[6]; EXEC SQL BEGIN DECLARE SECTION char c_sname[20]; short c_minrating; float c_age; EXEC SQL END DECLARE SECTION c_minrating = random(); EXEC SQL DECLARE sinfo CURSOR FOR SELECT S.sname, S.ageFROM Sailors S WHERE S.rating > :c_minrating ORDER BY S.sname; do { EXEC SQL FETCH sinfo INTO :c_sname, :c_age; printf(“%s is %d years old\n”, c_sname, c_age); } while (SQLSTATE != ‘02000’); EXEC SQL CLOSE sinfo;

6 Database Management Systems 6 Raghu Ramakrishnan Database APIs: Alternative to embedding v DBMS-independent v ODBC & JDBC v Database can be across a network

7 Database Management Systems 7 Raghu Ramakrishnan v Create the database in your dbms v Register the database as a data source v Import java.sql.* at the beginning of your java file. v Connect to a JDBC source Connection con = DriverManager.getConnection(“jdbc:oracle:thin:@coit- ora01:1521:class”,”user”,”passwd”) v Create an SQL statement Statement stmt = con.createStatement(); stmt.executeUpdate("INSERT INTO sailor VALUES(22,'dustin',7,45.0)"); JDBC steps

8 Database Management Systems 8 Raghu Ramakrishnan JDBC steps cont. v Execute the statement –ResultSet rs = stmt.executeQuery(“Select * from …”) v Parse the result – rs.next(), rs.getFloat –ResultSetMetaData contains the information about column v Close the statement and connection – stmt.close() – con.close

9 Database Management Systems 9 Raghu Ramakrishnan Useful resources v JDBC tutorial http://java.sun.com/docs/books/tutorial/jd bc/index.html http://java.sun.com/docs/books/tutorial/jd bc/index.html v UNCC COIT http://coit-servlet01.uncc.edu:8080/support/? v Example http://webpages/~xinzhang/Teaching/Sampl eJDBC.java http://webpages/~xinzhang/Teaching/Sampl eJDBC.java

10 Database Management Systems 10 Raghu Ramakrishnan SQL API in Java (JDBC) Connection con = // connect DriverManager.getConnection(url, ”login", ”pass"); Statement stmt = con.createStatement(); // set up stmt String query = "SELECT name, rating FROM Sailors"; ResultSet rs = stmt.executeQuery(query); try { // handle exceptions // loop through result tuples while (rs.next()) { String s = rs.getString(“name"); Int n = rs.getFloat(“rating"); System.out.println(s + " " + n); } } catch(SQLException ex) { System.out.println(ex.getMessage () + ex.getSQLState () + ex.getErrorCode ()); }


Download ppt "Database Management Systems 1 Raghu Ramakrishnan Database Application Development Chpt 6 Xin Zhang."

Similar presentations


Ads by Google