F23AF1: DATABASE   DB Access from a Programming Language: JDBC and JAVA Miss Jenny Coady Heriot-Watt University [email_add...
Database Access from a Programming Language <ul><li>Two Approaches </li></ul><ul><li>1.  Embedding SQL  into a programming...
Approach 1: Embedded SQL <ul><li>SQL code occurs  in program , separated by markers: </li></ul><ul><li>EXEC SQL SELECT ran...
Approach 1…. <ul><li>Transfer of values between PL and SQL:  </li></ul><ul><li>use of  host language variables  in SQL  (p...
Approach 2: Call Level Interfaces <ul><li>Principles of JDBC </li></ul><ul><li>Contact between  Java Application  (client)...
Schema of a JDBC Application <ul><li>Load the  driver  for a specific DBMS </li></ul><ul><li>(e.g., Oracle’s “Thin” driver...
JDBC Example Code: Parameters <ul><li>import java.io.*; </li></ul><ul><li>import java.sql.*; </li></ul><ul><li>// This cla...
Code (2) <ul><li>//URL of the DB: specifies access protocol and location of the DB </li></ul><ul><li>static String dburl1 ...
JDBC Example Code: Queries are String <ul><li>import java.io.*; </li></ul><ul><li>import java.sql.*; </li></ul><ul><li>Pub...
JDBC Example Code: Accessing the Database <ul><li>Public static void main (String args []) throws Exception { </li></ul><u...
Processing the result set <ul><li>//Process the result set with methods of class  ResultSet </li></ul><ul><li>public stati...
The Output <ul><li>java select </li></ul><ul><li>Adams  earns 1100 pm </li></ul><ul><li>Allen earns 1200 pm </li></ul><ul>...
Updates (1) <ul><li>import java.io.*; </li></ul><ul><li>import java.sql.*; </li></ul><ul><li>Public class Update extends O...
Updates (2) <ul><li>Public static void main (String args []) throws Exception { </li></ul><ul><li>Class.forName (driverNam...
 
JDBC: More Features <ul><li>Prepared Statements : updates and queries, with methods executeUpdate (), executeQuery () </li...
References <ul><li>These slides are partly based on learning materials provided by SUN Microsystems at: </li></ul><ul><li>...
Upcoming SlideShare
Loading in …5
×

JDBC / Java

709 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
709
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
22
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

JDBC / Java

  1. 1. F23AF1: DATABASE DB Access from a Programming Language: JDBC and JAVA Miss Jenny Coady Heriot-Watt University [email_address] Room: EMG37, Ext: 4178
  2. 2. Database Access from a Programming Language <ul><li>Two Approaches </li></ul><ul><li>1. Embedding SQL into a programming language </li></ul><ul><li>e.g., “Embedded SQL” for C and C++ </li></ul><ul><li>2. DB Access via API ( = “call level interface”) </li></ul><ul><li>e.g., JDBC, ODBC </li></ul><ul><li>~How do they work? </li></ul>
  3. 3. Approach 1: Embedded SQL <ul><li>SQL code occurs in program , separated by markers: </li></ul><ul><li>EXEC SQL SELECT ranking INTO :r </li></ul><ul><li>FROM sailors </li></ul><ul><li>WHERE sailors.sid = 15765; </li></ul><ul><li>r++; </li></ul><ul><li>EXEC SQL UPDATE sailors S </li></ul><ul><li>SET ranking = :r </li></ul><ul><li>WHERE sailors.sid = 15765; </li></ul>
  4. 4. Approach 1…. <ul><li>Transfer of values between PL and SQL: </li></ul><ul><li>use of host language variables in SQL (prefixed with “:”) </li></ul><ul><li>Compilation in two steps : </li></ul><ul><ul><li>Preprocessor translates SQL fragments into function calls of SQL run time library (= pure c/c++ code, depends on DBMS) </li></ul></ul><ul><ul><li>Regular compiler for C/C++ produces executable </li></ul></ul>
  5. 5. Approach 2: Call Level Interfaces <ul><li>Principles of JDBC </li></ul><ul><li>Contact between Java Application (client) and database (server) is managed by a “driver” </li></ul><ul><li>Drivers are vendor specific </li></ul><ul><li>e.g., drivers for Oracle, DB2, SQL Server, … </li></ul><ul><li>Client sends strings , driver returns sets of tuples </li></ul><ul><li>Driver communicates with DBMS </li></ul><ul><li>~ compiled client does not contain compiled SQL code </li></ul><ul><li>~ one client can communicate with many DBMS’s, even from different vendors </li></ul>
  6. 6. Schema of a JDBC Application <ul><li>Load the driver for a specific DBMS </li></ul><ul><li>(e.g., Oracle’s “Thin” driver) </li></ul><ul><li>Establish a connection to a specific database </li></ul><ul><li>(e.g., the orcl database on oracle) </li></ul><ul><li>Create an abstract statement , to be sent over the connection </li></ul><ul><li>Execute the statement by sending a Java string </li></ul><ul><li>(e.g., “SELECT ranking FROM sailors WHERE sid = 15765”) </li></ul><ul><li>~ returns an object of class ResultSet </li></ul><ul><li>Process the result set with the methods of ResultSet </li></ul><ul><li>Close statement and connection </li></ul>
  7. 7. JDBC Example Code: Parameters <ul><li>import java.io.*; </li></ul><ul><li>import java.sql.*; </li></ul><ul><li>// This class collects the parameters for the example </li></ul><ul><li>public class OracleAccess { </li></ul><ul><li>static PrintWriter screen = new PrintWriter(system.out, true); </li></ul><ul><li>static BufferedReader keyboard = </li></ul><ul><li>new BufferedReader (new InputStreamReader (System.in)); </li></ul><ul><li>// Driver name: set CLASSPATH so that Java can find it! </li></ul><ul><li>static String driverName = “oracle.jdbc.driver.OracleDriver”; </li></ul>
  8. 8. Code (2) <ul><li>//URL of the DB: specifies access protocol and location of the DB </li></ul><ul><li>static String dburl1 = “jdbc:oracle:thin:@oracle:1521:orcl”; </li></ul><ul><li>// User name: it is jenny even if the login is [email_address] </li></ul><ul><li>static String user = “jenny”; </li></ul><ul><li>// Password for the database </li></ul><ul><li>static String passwd = “Hello”; </li></ul><ul><li>} </li></ul>
  9. 9. JDBC Example Code: Queries are String <ul><li>import java.io.*; </li></ul><ul><li>import java.sql.*; </li></ul><ul><li>Public class Select extends OracleAccess { </li></ul><ul><li>// Our example Query </li></ul><ul><li>static String selectQuery = </li></ul><ul><li>“ SELECT ename, sal “ + </li></ul><ul><li>“ FROM emp” + </li></ul><ul><li>“ WHERE hiredate > ’01-Jan-1980’” </li></ul><ul><li>“ ORDER BY ename”; </li></ul><ul><li>This is the query that we want to send to the database! </li></ul>
  10. 10. JDBC Example Code: Accessing the Database <ul><li>Public static void main (String args []) throws Exception { </li></ul><ul><li>// Load the Driver </li></ul><ul><li>Class.forName (driverName); </li></ul><ul><li>// Establish a connection to the database via the driver </li></ul><ul><li>Connection con = </li></ul><ul><li>DriverManager.getConnection(dburl1, user, passwd); </li></ul><ul><li>//Create an abstract statement for the connection </li></ul><ul><li>Statement stmt = con.createStatement (); </li></ul><ul><li>//Execute the query and retrieve the set of results </li></ul><ul><li>ResultSet rs = stmt.executeQuery (selectQuery); </li></ul><ul><li>//Process results </li></ul><ul><li>printResults (rs, screen); </li></ul><ul><li>//Close statement and connection </li></ul><ul><li>stmt.close (); </li></ul><ul><li>con.close (); </li></ul><ul><li>} </li></ul>
  11. 11. Processing the result set <ul><li>//Process the result set with methods of class ResultSet </li></ul><ul><li>public static void printResults (ResultSet rs, PrintWriter pw) { </li></ul><ul><li>try { </li></ul><ul><li>//Move cursor over the result set </li></ul><ul><li>while (rs.next ()) { </li></ul><ul><li>//Fetch components of result tuples </li></ul><ul><li>String ename = rs.getString (1); </li></ul><ul><li>int sal = re.getInt (2); </li></ul><ul><li>//Feed components into computation </li></ul><ul><li>scree.println(ename + “ earns” + sal + “ pm”); } </li></ul><ul><li>//Close result set </li></ul><ul><li>rs.close (); </li></ul><ul><li>// Catch exceptions </li></ul><ul><li>} catch (Exception e) { </li></ul><ul><li>screen.println(e.toString ());} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  12. 12. The Output <ul><li>java select </li></ul><ul><li>Adams earns 1100 pm </li></ul><ul><li>Allen earns 1200 pm </li></ul><ul><li>Clark earns 2450 pm </li></ul><ul><li>Ford earns 850 pm </li></ul><ul><li>Turner earns 2150 pm </li></ul><ul><li>Ward earns 1100 pm </li></ul><ul><li>Process Select finished </li></ul>
  13. 13. Updates (1) <ul><li>import java.io.*; </li></ul><ul><li>import java.sql.*; </li></ul><ul><li>Public class Update extends OracleAccess { </li></ul><ul><li>// Our example Query </li></ul><ul><li>static String update = </li></ul><ul><li>“ INSERT INTO emp VALUES (“+ </li></ul><ul><li>“ 8492, ‘MacGregor’, ‘Clerk’, 7902, “+ </li></ul><ul><li>“’ 15 Nov 2001’, 18000, null, 200)”; </li></ul>
  14. 14. Updates (2) <ul><li>Public static void main (String args []) throws Exception { </li></ul><ul><li>Class.forName (driverName); </li></ul><ul><li>Connection con = </li></ul><ul><li>DriverManager.getConnection(dburl1, user, passwd); </li></ul><ul><li>Statement stmt = con.createStatement (); </li></ul><ul><li>//Execute the update </li></ul><ul><li>stmt.executeUpdate (update); </li></ul><ul><li>stmt.close (); </li></ul><ul><li>con.close (); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  15. 16. JDBC: More Features <ul><li>Prepared Statements : updates and queries, with methods executeUpdate (), executeQuery () </li></ul><ul><li>Navigation in result sets: </li></ul><ul><ul><li>Forward, backward, skip n tuples, … </li></ul></ul><ul><li>Transactions </li></ul><ul><ul><li>Defined for connections </li></ul></ul><ul><ul><li>Method commit (), rollback (),… </li></ul></ul><ul><li>Error handling with classes </li></ul><ul><ul><li>SQLException </li></ul></ul><ul><ul><li>SQLWarning </li></ul></ul>
  16. 17. References <ul><li>These slides are partly based on learning materials provided by SUN Microsystems at: </li></ul><ul><li>http://java.sun.com/products/jdbc/learning.html </li></ul><ul><li>The examples on the slides have been run on the departmental Linux machines and reflect our local setup. </li></ul>

×