Presentation is loading. Please wait.

Presentation is loading. Please wait.

Helia / Martti Laiho, 1998-2000 JDBC Java Database Connection.

Similar presentations


Presentation on theme: "Helia / Martti Laiho, 1998-2000 JDBC Java Database Connection."— Presentation transcript:

1 Helia / Martti Laiho, JDBC Java Database Connection

2 Helia / Martti Laiho, Notes on JDBC - Java Database Connection Class Library: java.sql.* Literature/sources: SunSoft: JDBC Specification jdk1.3/docs/guide/jdbc/ JDBC Guide: Getting Started Seth White & al: JDBC TM API Tutorial and Reference, 2nd ed Horstmann & Cornell: Core JAVA Volume II Chapter 4 Orfali & Harkey: Client/Server Programming with JAVA and CORBA Siple: The Complete Guide to JAVA Database Programming, McGraw-Hill SOLID JDBC: sj23win.zip SOLID JDBC Driver Programmer’s Guide Melton & Eisenberg: Understanding SQL and Java Together

3 Helia / Martti Laiho, JDBC 1.0 API Designed by JavaSoft based on ISO SQL/CLI and Microsoft ODBC API provided in java.sql package 4 types of JDBC Driver implementation

4 Helia / Martti Laiho, Types of JDBC Implementations Java appl JDBC-ODBC bridge ODBC driver DBMS Java appl JDBC driver Native db-library DBMS Java appl JDBC server gateway DBMS Java appl DBMS Proprietary protocol Proprietary protocol JDBC driver JDBC driver DBMS- independent protocol DBMS- specific protocol Type 1 Type 2Type 3Type 4 - Melton & Eisenberg - Oracle JDBC/OCI - Oracle Thin JDBC - Sybase jConnect - Solid

5 Helia / Martti Laiho, SQL and Java data types SQL data type: INT[EGER] SMALLINT NUMERIC (m, n) DECIMAL (m, n) DEC (m, n) FLOAT (n) REAL DOUBLE CHAR[ACTER] (n) VARCHAR (n) DATE TIME TIMESTAMP Java data type: int short java.sql.BigDecimal double float double String java.sql.Date java.sql.Time java.sql.Timestamp

6 Helia / Martti Laiho, Java.sql - Interfaces / Methods... getMetaData() setAutoCommit(b) setTransaction Isolation(level) createStatement() prepareStatement(sql) prepareCall(sql) commit() rollback() close() setCursorName(s) executeQuery(sql) executeUpdate(sql) cancel() close() getMetaData() findColumn(name) next() getInt(col) getShort(col) getNumeric(col) getDouble(col) getFloat(col) getString(col) getDate(col) getTime(col) getTimestamp(col) wasNull() setText(s) append(s) close() SQLException getSQLState() getErrorCode() getNextExcetion() Driver Connection Statement ResultSetResultSetMetaData getColumnCount() getColumnName(i) getColumnLabel(i) getColumnDisplaySize(i) DatabaseMetaData getTables(…) … PreparedStatement … setXxxx(n, hvar) clearParameters() getConnection (url, user, psw) DriverManager Class CallableStatement registerOutputParameter execute()... getConnection (url, user, psw) DataSource

7 Helia / Martti Laiho, SQL Query String s; float n;... String query = "SELECT COF_NAME, PRICE FROM COFFEES"; ResultSet rs = stmt.executeQuery(query); while (rs.next()) { s = rs.getString("COF_NAME"); n = rs.getFloat("PRICE"); System.out.println(s + " " + n); } rs.close; COF_NAME PRICE snsn rs.next() rs.getString() rs.getFloat()

8 Helia / Martti Laiho, SQLQuery Sequence Diagram Client DriverManager Connection Statement ResultSet getConnection createStatement next getString getInt... close executeQuery adapted from Orfali & Harkey { [ Until next returns false ] }

9 Helia / Martti Laiho, Invoking a Stored Procedure Client DriverManager Connection Callable Statement Callable Statement getConnection prepareCall getString getInt... close execute... adapted from Orfali & Harkey parameters marked in the procedures call by ? placeholders are identified by the corresponding order numbers 1, 2,.. of the placeholders registerOutputParameter

10 Helia / Martti Laiho, JDBC Escape Syntax call{call proc (arg1, …) } ?=call{?= call proc (arg1, …) } d{d ‘yyyy-mm-dd’} escape{escape ‘%’} fn{fn function (arg1, …) } oj{oj outer-join } t{t ‘hh:mm:ss’} ts{ts ‘yyyy-mm-dd hh:mm:ss.fffff’}

11 Helia / Martti Laiho, Transactions Default: AutoCommit Isolation Levels: 0TRANSACTION_NONE 1TRANSACTION_READ_UNCOMMITTED 2TRANSACTION_READ_COMMITTED 3TRANSACTION_REAPEATABLE_READ 4TRANSACTION_SERIALIZABLE Methods: con.setAutoCommit(false); level = con.getTransactionIsolation(); con.setTransactionIsolation(level); con.commit(); con.rollback();

12 Helia / Martti Laiho, Exception handling try { jdbc method call... } catch (SQLException ex) { System.out.println (”\nSQLException:"); while (ex != null) { System.out.println (”SQLState: "+ex.getSQLState()); System.out.println (”Message: "+ ex.getMessage()); System.out.println (”Vendor: "+ ex.getErrorCode()); ex = ex.getNextException(); } catch (java.lang.Exception ex) { System.out.println("Exception: " + ex); ex.printStackTrace (); } - adapted from Core JAVA Vol II ch 4 p 206

13 Helia / Martti Laiho, JDBC 2.0 API JDBC 2.0 Core API (java.sql) –Scrollable ResultSet –Updating by ResultSet –Batch Updates –New SQL-99 datatypes JDBC 2.0 Standard Extension API (javax.sql)

14 Helia / Martti Laiho, Scrollable ResultSet Resultset types –TYPE_FORWARD_ONLY (~JDBC 1.0) –TYPE_SCROLL_INSENSITIVE –TYPE_SCROLL_SENSITIVE Methods –beforeFirst() (initially) –first() –next() ( JDBC 1.0) –previous() –last() –afterLast() –absolute (n | -n) –relative (n | -n) –getRow() –isFirst(), isLast(), isBeforeFirst(), isAfterLast() –moveToInsertRow(), moveToCurrentRow()

15 Helia / Martti Laiho, Updatable ResultSet Updatable –CONCUR_READ_ONLY (~JDBC 1.0) –CONCUR_UPDATABLE Methods –updateXXX(column, value) –… –updateRow() or cancelRowUpdates()

16 Helia / Martti Laiho, Inserting a new row InsertRow processing: –moveToInsertRow() –updateXXX(, ) …. –insertRow() –moveToCurrentRow() updateable row “Current row” “InsertRow buffer” moveToInsertRow() moveToCurrentRow() InsertRow() ResultSet:

17 Helia / Martti Laiho, Deleting a Row Positioning in the ResultSet and deleting: – –deleteRow() Note: –drivers handle deletions differently

18 Helia / Martti Laiho, Refreshing the row Applies only to Cursor type: –TYPE_SCROLL_SENSITIVE method –refreshRow()

19 Helia / Martti Laiho, Batch Updates Methods –addBatch(“….”) –… –executeBatch(); BatchUpdateException

20 Helia / Martti Laiho, SQL-1999 Datatypes BLOB - binary large objects CLOB - character large objects SQL Array - of any SQL scalar datatype SQL structured type - User Defined Type UDT SQL REF - identifier

21 Helia / Martti Laiho, JDBC 2.0 Standard Extension API JDBC 2.0 Standard Extension API i.e. Optional Package API –in javax.sql –JavaBeans: Rowsets –JNDI for naming and directory interface –Connection Pooling –Distributed Transactions: 2PC by Java Transaction API (JTA)


Download ppt "Helia / Martti Laiho, 1998-2000 JDBC Java Database Connection."

Similar presentations


Ads by Google