SQL, Embedded SQL, JDBC

part III :: JDBC

CSE3421 notes

1 JDBC (Java Connectivity) • A standardized means by which java programs can access . • JDBC relies on 2 software components: – Set of interfaces (and a few classes) defined in java.. – JDBC database drivers to carry out the tasks defined in java.sql against popular DBMSs.

2 DB JDBC access architecture

Java program Supplied by Java programmer. Makes calls to methods from java.sql package.

JDBC interfaces in java.sql Supplied by java API

Supplied by DB vendors (and other vendors). JDBC driver This is software that implements the java.sql interfaces (for different DBMSs).

SQL DBMS Database 3 • The Java API contains the java.sql package, which is mostly interfaces. • Software vendors produce code that implements the java.sql interfaces (for different DBMSs). This code is sold (or freely distributed …) in the form of ‘JDBC drivers’. For example, a JDBC driver for DB2 contains the implementation of all interfaces in java.sql and also the ‘know how’ to connect to a DB2 database. • Java programmers write code that calls the java.sql methods (relying on the implementations provided by the supplied driver).

4 • The JDBC interfaces in java.sql act as a standard in 2 ways; – For DB application programmers: have a single DB access technique to learn (for many DB brands). – For JDBC driver developers: have a single set of methods to implement (that meets the expectations of a large group of DB application developers).

5 Using JDBC in your Java programs

user 1 Java application

4

3 2 Display SQL response Response. queries (on UI ..) (Result Set)

DB

6 1. USER: you write code in Java and SQL that tells the JDBC driver what you want to do with the DB. 2. The JDBC driver figures out the details and communicates with the DB to accomplish your request. 3. The driver receives a response (which may be an exception …) from the DB, after carrying out the request; it then passes the response to your program. 4. Your program reacts appropriately (e.g., displays on UI or handles exception).

7 Coding with JDBC – the SIX steps

The SIX steps that a Java program must carry out to use JDBC are as follows: Step 1: (do this once) Your program obtains the class name of the JDBC driver that is to be used. Your program then loads the driver.

Class.forName (drivername );

8 • Driver name for this course:

COM..db2.jdbc.app.DB2Driver

9 Step 2

Step 2 (do this once) : your program obtains a suitable URL that designates the database you want to access. The URL must be written in a form that can be understood by the driver.

URL: jdbc.db2.c3421m

10 Step 3 Step 3 (do this once): your program requests the DriverManager to establish a connection to the database.

// steps 2 and 3 code Connection con; String url = “jdbc:db2:c3421m”; Con = DriverManager.getConnection(url);

11 Step 4 Step 4: your program formulates a SQL statement to be executed against the database. Your program then requests the driver to transmit the statement to the database, and awaits the response.

12 Step 5 Step 5: your program processes the response. The response could be:

 a result set (rows of data from the DB), or  a count of the number of rows in the DB affected by the statement just executed, or  an error messages (exception).

Your program must detect what sort of response the DB provided and it must react sensibly.

13 How to detect the type of response

ResultSetMetaData rsmd; rsmd = rs.getMetaData(); ResultSet rs; should be And then: defined earlier. nCol = rsmd.getColumnCount();

Number of columns in ResultSet. Columns are numbered 1, 2, …

14 Example while (rs.next()) { for (int col=1; col <= nCol; col++) { String s = rs.getString(col); } Get value of column as a Or .. Java String. String s = rsmd.getColumnLabel(col);

The current column’s title (for printing…). 15 Related methods (but there are more .. These are the most common ones). • getConnection(String) • createStatement() • executeQuery(String) (*****) • Execute(String) • getMetaData() • getColumnLabel(int) • Close() • getColumnDisplaySize(int)

16 (*****)

SQL Method in JDBC Result Type SELECT executeQuery ResultSet INSERT executeUpdate int

UPDATE executeUpdate int

DELETE executeUpdate int

Number of rows affected

17 Step 6

Step 6: when finished with the connection, your program closes it.

con.close();

(where, Connection con; has been defined before).

18 In a typical Java program … A typical Java program that uses JDBC,

• steps 1, 2, 3, and 6 are usually carried out just once. • Steps 4 and 5 are usually carried out several times.

19 See posted on web site … • Posted on the web site is a Java program that: – Connects to DB2 – Creates a query – Sends the query to DB2

(creates a table and then inserts some data into that table).

Feel free to download …

20 The end of part III (JDBC ) of SQL, Embedded SQL, JDBC

21