Database Application Development - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Database Application Development

Description:

Cursors. Can declare a cursor on a relation or query statement (which generates a ... which orders answer tuples, is only allowed in the context of a cursor. ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 11
Provided by: RaghuRama114
Category:

less

Transcript and Presenter's Notes

Title: Database Application Development


1
Database Application Development
  • Chpt 6

Xin Zhang
2
Embedded SQL
  • 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.
  • SQL relations are set-oriented.
  • SQL supports a mechanism called cursor to handle
    this.

3
Cursors
  • Can declare a cursor on a relation or query
    statement (which generates a relation).
  • 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.
  • 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.
  • Can also modify/delete tuple pointed to by a
    cursor.

4
Cursor Declaration
Find names, and ages of those sailors whos
rating is above 7, in alphabetical order
EXEC SQL DECLARE sinfo CURSOR FOR SELECT
S.sname FROM Sailors S WHERE rating gt7 ORDER
BY S.sname
5
Embedding SQL in C An Example
  • char SQLSTATE6
  • EXEC SQL BEGIN DECLARE SECTION
  • char c_sname20 short c_minrating float c_age
  • EXEC SQL END DECLARE SECTION
  • c_minrating random()
  • EXEC SQL DECLARE sinfo CURSOR FOR
  • SELECT S.sname, S.age FROM Sailors S
  • WHERE S.rating gt 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 APIs Alternative to embedding
  • DBMS-independent
  • ODBC JDBC
  • Database can be across a network

7
JDBC steps
  • Create the database in your dbms
  • Register the database as a data source
  • Import java.sql. at the beginning of your java
    file.
  • Connect to a JDBC source
  • Connection con DriverManager.getConnection(jdbc
    oraclethin_at_coit-ora011521class,user,passw
    d)
  • Create an SQL statement
  • Statement stmt con.createStatement()
  • stmt.executeUpdate("INSERT INTO sailor
    VALUES(22,'dustin',7,45.0)")

8
JDBC steps cont.
  • Execute the statement
  • ResultSet rs stmt.executeQuery(Select from
    )
  • Parse the result
  • rs.next(), rs.getFloat
  • ResultSetMetaData contains the information about
    column
  • Close the statement and connection
  • stmt.close()
  • con.close

9
Useful resources
  • JDBC tutorial http//java.sun.com/docs/books/tutor
    ial/jdbc/index.html
  • UNCC COIT
  • http//coit-servlet01.uncc.edu8080/support/?
  • Example http//webpages/xinzhang/Teaching/SampleJ
    DBC.java

10
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
    ())
Write a Comment
User Comments (0)
About PowerShow.com