Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
JDBC Java Database Connection Helia / Martti Laiho, 1998-2000 Notes on JDBC - Java Database Connection Class Library: java.sql.* Literature/sources: • SunSoft: http://java.sun.com/products/jdbc JDBC Specification • jdk1.3/docs/guide/jdbc/ JDBC Guide: Getting Started • Seth White & al: JDBCTM 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 Helia / Martti Laiho, 1998-2000 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 Helia / Martti Laiho, 1998-2000 Types of JDBC Implementations - Melton & Eisenberg Type 1 Type 2 Type 3 Type 4 Java appl Java appl Java appl Java appl JDBC-ODBC bridge JDBC driver JDBC driver JDBC driver ODBC driver Native db-library Proprietary protocol DBMS Proprietary protocol DBMS - Oracle JDBC/OCI DBMS- independent protocol DBMS- specific protocol JDBC server gateway DBMS DBMS - Oracle Thin JDBC - Sybase jConnect - Solid Helia / Martti Laiho, 1998-2000 SQL and Java data types SQL data type: Java 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 int short java.sql.BigDecimal java.sql.BigDecimal java.sql.BigDecimal double float double String String java.sql.Date java.sql.Time java.sql.Timestamp Helia / Martti Laiho, 1998-2000 Java.sql - Interfaces / Methods DatabaseMetaData DataSource getTables(…) … getConnection (url, user, psw) DriverManager Class getConnection (url, user, psw) Driver Connection Statement getMetaData() setAutoCommit(b) setTransaction Isolation(level) createStatement() prepareStatement(sql) prepareCall(sql) commit() rollback() close() setCursorName(s) executeQuery(sql) executeUpdate(sql) cancel() close() ... PreparedStatement … setXxxx(n, hvar) clearParameters() SQLException CallableStatement getSQLState() getErrorCode() getNextExcetion() registerOutputParameter execute() ... ResultSet 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() ResultSetMetaData getColumnCount() getColumnName(i) getColumnLabel(i) getColumnDisplaySize(i) Helia / Martti Laiho, 1998-2000 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"); rs.next() COF_NAME PRICE n = rs.getFloat("PRICE"); System.out.println(s + " " + n); } rs.getString() rs.close; s n rs.getFloat() Helia / Martti Laiho, 1998-2000 SQLQuery Sequence Diagram Client adapted from Orfali & Harkey DriverManager getConnection Connection createStatement Statement executeQuery ResultSet next getString getInt ... { [ Until next returns false ] } close close close Helia / Martti Laiho, 1998-2000 Invoking a Stored Procedure Client adapted from Orfali & Harkey DriverManager getConnection Connection prepareCall Callable Statement registerOutputParameter ... execute parameters marked in the procedures call by ? placeholders are identified by the corresponding order numbers 1, 2, .. of the placeholders getString getInt ... close close Helia / Martti Laiho, 1998-2000 JDBC Escape Syntax call ?=call d escape fn oj t ts {call proc (arg1, …) } {?= call proc (arg1, …) } {d ‘yyyy-mm-dd’} {escape ‘%’} {fn function (arg1, …) } {oj outer-join } {t ‘hh:mm:ss’} {ts ‘yyyy-mm-dd hh:mm:ss.fffff’} Helia / Martti Laiho, 1998-2000 Transactions Default: AutoCommit Isolation Levels: 0 TRANSACTION_NONE 1 TRANSACTION_READ_UNCOMMITTED 2 TRANSACTION_READ_COMMITTED 3 TRANSACTION_REAPEATABLE_READ 4 TRANSACTION_SERIALIZABLE Methods: con.setAutoCommit(false); level = con.getTransactionIsolation(); con.setTransactionIsolation(level); con.commit(); con.rollback(); Helia / Martti Laiho, 1998-2000 Exception handling - adapted from Core JAVA Vol II ch 4 p 206 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 (); } Helia / Martti Laiho, 1998-2000 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) Helia / Martti Laiho, 1998-2000 Scrollable ResultSet • Resultset types – TYPE_FORWARD_ONLY – TYPE_SCROLL_INSENSITIVE – TYPE_SCROLL_SENSITIVE (~JDBC 1.0) • Methods – – – – – – – – – – – beforeFirst() (initially) first() next() (JDBC 1.0) previous() last() afterLast() absolute (n | -n) relative (n | -n) getRow() isFirst() , isLast() , isBeforeFirst() , isAfterLast() moveToInsertRow(), moveToCurrentRow() Helia / Martti Laiho, 1998-2000 Updatable ResultSet • Updatable – CONCUR_READ_ONLY (~JDBC 1.0) – CONCUR_UPDATABLE • Methods – updateXXX(column, value) – … – updateRow() or cancelRowUpdates() Helia / Martti Laiho, 1998-2000 Inserting a new row • InsertRow processing: ResultSet: – moveToInsertRow() – updateXXX( , ) …. – insertRow() “Current row” – moveToCurrentRow() moveToCurrentRow() updateable row moveToInsertRow() InsertRow() “InsertRow buffer” Helia / Martti Laiho, 1998-2000 Deleting a Row • Positioning in the ResultSet and deleting: – <move method> – deleteRow() • Note: – drivers handle deletions differently Helia / Martti Laiho, 1998-2000 Refreshing the row • Applies only to Cursor type: – TYPE_SCROLL_SENSITIVE • method – refreshRow() Helia / Martti Laiho, 1998-2000 Batch Updates • Methods – addBatch(“….”) – … – executeBatch(); • BatchUpdateException Helia / Martti Laiho, 1998-2000 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 Helia / Martti Laiho, 1998-2000 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) Helia / Martti Laiho, 1998-2000