Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Jdbc 4.0 New Features And Enhancements


Published on

Presented in Denver Java Users Group( meeting on May 9, 2007

Jdbc 4.0 New Features And Enhancements

  1. 1. JDBC 4.0 New Features and Enhancements By Sharad Acharya Cabela’s Inc May 9, 2007
  2. 2. Goal of the presentation <ul><li>Describe and discuss JDBC4.0 APIs </li></ul><ul><ul><li>objectives </li></ul></ul><ul><ul><li>new features </li></ul></ul><ul><ul><li>enhancements </li></ul></ul>
  3. 3. <ul><li>Introduction </li></ul><ul><li>New features and enhancements </li></ul><ul><li>Code examples </li></ul><ul><li>Key points </li></ul><ul><li>Q/A </li></ul>Structure of this presentation
  4. 4. Introduction to JDBC <ul><li>Set of API's that allow Java programs to </li></ul><ul><li>Create, retrieve, delete and update RDBMS </li></ul><ul><li>Send SQL statements to underlying database and retrieve results </li></ul>
  5. 5. JDK and JDBC releases 9/2004 5.0(Tiger) 12/2006 4.0 6.0(Mustang) 7.0(Dolphin?) 2/2002 3.0 1.4(Merlin) 5/2000 1.3(Kestrel) 12/1998 2.0 1.2(Playground) 9/1997 1.0 1.1(Brutus, Chelsea…) Date JDBC JDK(codename)
  6. 6. JDBC 4.0 <ul><li>Released with JSDK 6.0(AKA Mustang) </li></ul><ul><li>Java Specification Request (JSR) 221 under Java Community Process(JCP). </li></ul><ul><li>Goals: </li></ul><ul><ul><li>better object management support </li></ul></ul><ul><ul><li>more data type support </li></ul></ul><ul><ul><li>flexible </li></ul></ul><ul><ul><li>ease of use </li></ul></ul>
  7. 7. JDBC 4.0 <ul><li>API Specification lists twenty new features or enhancements </li></ul><ul><li>Some are major: SQLXML data type </li></ul><ul><li>Some are minor: a new free() method in Array interface </li></ul><ul><li>New APIs provide access to commonly implemented features of SQL:2003 </li></ul>
  8. 8. <ul><li>Introduction </li></ul><ul><li>New features and enhancements </li></ul><ul><li>Examples </li></ul><ul><li>Key points </li></ul><ul><li>Q/A </li></ul>Structure of this presentation
  9. 9. What is new? <ul><li>Object management </li></ul><ul><li>Exception handling </li></ul><ul><li>Data type support </li></ul><ul><li>API changes </li></ul>
  10. 10. Easy driver management <ul><li>‘ Getting connected’ becomes easier </li></ul><ul><ul><li>Goodbye to Class.forName(“jdbc_class&quot;) </li></ul></ul><ul><ul><li>Welcome to automatic driver class loading </li></ul></ul><ul><ul><li>DataSource object </li></ul></ul><ul><ul><ul><li>preferred way </li></ul></ul></ul><ul><ul><ul><li>promotes portability </li></ul></ul></ul><ul><ul><ul><li>allows dynamic driver usage </li></ul></ul></ul>
  11. 11. Code snippet 1 <ul><li>Getting connection in Apache derby database </li></ul><ul><ul><li>DataSource ds = new EmbeddedDataSource40(); </li></ul></ul><ul><ul><li>ds.setDatabaseName(“dbName”); </li></ul></ul><ul><ul><li>con = ds.getConnection(); </li></ul></ul>
  12. 12. More flexible ResultSet <ul><li>SQLXML data type support(4.0) </li></ul><ul><li>RowSet : scrollable, updateable, offline editable ResultSet(3.0) </li></ul><ul><li>WebRowSet : read data from database tables and serialize to XML document and vice versa (3.0) </li></ul>
  13. 13. Exception handling <ul><li>Chained exception </li></ul><ul><li>Enhanced for loop support </li></ul><ul><ul><li>try {//some code that throw SQLException } </li></ul></ul><ul><ul><li>catch (SQLException ex) { </li></ul></ul><ul><ul><ul><li>for(Throwable t : ex ) { </li></ul></ul></ul><ul><ul><ul><li>System.err.println(&quot;Error encountered: &quot; + t); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>} </li></ul></ul>
  14. 14. Data type support <ul><li>New SQLXML data </li></ul><ul><li>New RowId data </li></ul><ul><li>Enhanced support for large object types </li></ul>
  15. 15. New SQLXML data type <ul><li>Mapping in the Java programming language for the XML data type in SQL </li></ul><ul><li>Stores an XML value in a table column </li></ul><ul><li>Implemented as a logical pointer to the XML data </li></ul><ul><li>valid for the duration of the transaction in which it was created </li></ul>
  16. 16. Code snippet 2 <ul><li>con = ds.getConnection(); </li></ul><ul><li>SQLXML sx= con.createSQLXML(); </li></ul><ul><li>sx.setString(“Catalog&quot;); </li></ul><ul><li>String psx =&quot;insert into &quot;+tableName+ </li></ul><ul><li>&quot; ( orderNo, orderType) values(?,?) &quot;; </li></ul><ul><li>PreparedStatement pstmt = con.prepareStatement(psx); </li></ul><ul><li>pstmt.setString(1,“1122344&quot;); </li></ul><ul><li>pstmt.setSQLXML(2,sx); </li></ul><ul><li>pstmt.executeUpdate(); </li></ul>
  17. 17. Scenario: Order entry system <ul><li>JMS integration, XML for message exchange </li></ul><ul><li>Substantial code to convert data from/to XML </li></ul><Legacy> Order entry <database> Oracle I N T E G R A T I O N <Legacy> Inventory Management <Legacy> Order dispatch <J2EE TX> Order process JDBC
  18. 18. Code snippet 3.1 <ul><li>Document doc= Util.createDoc(…) </li></ul><ul><li>String queryString = “select id, orderNo from…”; </li></ul><ul><li>String orderNo=rs.getString(2); </li></ul><ul><li>Element ele = Util.createElement(…); </li></ul><ul><li>//…. Prepare the xml(6 more lines) </li></ul><ul><li>Util.appendChild(doc, ele); </li></ul><ul><li>//client code 15 lines </li></ul>
  19. 19. Code snippet 3.2 SQLXML <ul><li>SQLXML sqlxml = rs.getSQLXML(column); </li></ul><ul><li>InputStream bis = sqlxml.getBinaryStream(); </li></ul><ul><li>DocumentBuilder parser = DocumentBuilderFactory.newInstance(). </li></ul><ul><li>newDocumentBuilder(); </li></ul><ul><li>Document doc = parser.parse(binaryStream); </li></ul>
  20. 20. RowId class <ul><li>SQL ROWID identifies a row within a table and is the fastest way to access it. </li></ul><ul><li>A new RowId interface provides access to ROWID SQL type from a Java class. </li></ul>
  21. 21. Large objects <ul><li>CLOB, BOLB, ARRAY and STRUCT </li></ul><ul><li>Interface Clob, Blob, Array and Struct were added on Version 2.0 of JDBC </li></ul><ul><li>More methods added in version 4.0 for large object manipulation </li></ul>
  22. 22. API changes <ul><li>Array </li></ul><ul><li>Connection </li></ul><ul><li>DatabaseMetaData </li></ul><ul><li>NCS support </li></ul><ul><li>Scalar Functions </li></ul><ul><li>Statement and its sub interfaces </li></ul><ul><li>Wrapper </li></ul>
  23. 23. Array <ul><li>The mapping in the Java programming language for the SQL type ARRAY </li></ul><ul><li>Since JDK 1.2 </li></ul><ul><li>JDBC 4.0 adds a new free() method </li></ul>
  24. 24. Connection <ul><li>Several create methods createClob, createBlob to create large objects in Connection interface. </li></ul><ul><li>Method to register/deregister StatementEventListener in PooledConnection interface </li></ul>
  25. 25. DatabaseMetaData <ul><li>Code portability issues </li></ul><ul><li>Need interface to query database capabilities, if provided </li></ul><ul><li>Example: CREATE TABLE </li></ul><ul><li>getTypeInfo may be called to find out if a feature is supported before issuing an execute command </li></ul>
  26. 26. Code snippet 3 <ul><li>List database schemas under a catalog that match a pattern(new in JDBC 4.0) </li></ul><ul><ul><li>con = ds.getConnection(); </li></ul></ul><ul><ul><li>DatabaseMetaData dmd = con.getMetaData(); </li></ul></ul><ul><ul><li>rs=dmd.getSchemas( </li></ul></ul><ul><ul><li>&quot;TABLE_CAT&quot;,&quot;SYS%&quot;); </li></ul></ul>
  27. 27. Portability <ul><li>Backward compatible </li></ul>
  28. 28. National Character Set(NCS) <ul><li>SQL:2003 provides support for SQL types of NCHAR, NVARCHAR, LONGNVARCHAR and NCLOB </li></ul><ul><li>Choose NCS if data need extensive character processing operations </li></ul><ul><li>JDBC 4.0 provides NCS support </li></ul>
  29. 29. API to support NCS <ul><li>Setter and update methods in PreparedStatement and CallableStatement </li></ul><ul><li>Setter methods in ResultSet eg, setNString, setNClob </li></ul><ul><li>Read and write methods in SQLInput and SQLOuput, eg readNClob, writeNClob, readNString, writeNString </li></ul>
  30. 30. Scalar Function <ul><li>Operates on some input and returns some result : abs(number) </li></ul><ul><li>Used in SQL query from Java code </li></ul><ul><li>JDBC 4.0 supports for additional scalar functions: </li></ul><ul><ul><li>CHAR_LENGTH, CHARACTER_LENGTH, CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP, EXTRACT, OCTET_LENGTH, POSITION </li></ul></ul>
  31. 31. Statements <ul><li>Statement </li></ul><ul><ul><li>isClosed, setPoolable, isPoolable </li></ul></ul><ul><li>PreparedStatement and CallableStatement: </li></ul><ul><ul><li>more methods to insert large objects using InputStream and Reader </li></ul></ul>
  32. 32. Wrapper <ul><li>Vendors may wrap implementation of JDBC driver for architectural reason </li></ul><ul><li>provide standard access to wrapped resource </li></ul><ul><li>Implemented as Adapter Pattern </li></ul>
  33. 33. Using Wrapper <ul><li>New interface in JDBC 4.0 </li></ul><ul><li>Use isWrappedFor(Class<?> iface) to check if the class that implements this interface is either direct or indirect wrapper for an object </li></ul><ul><li>find out an object that implements the given interface by unwrap(Class <T> iface) </li></ul>
  34. 34. <ul><li>Introduction </li></ul><ul><li>New features and enhancements </li></ul><ul><li>Examples </li></ul><ul><li>Key points </li></ul><ul><li>Q/A </li></ul>Structure of this presentation
  35. 35. Example1 <ul><li>Retrieving connection </li></ul><ul><li>Previous versions vs Version 4.0 </li></ul>
  36. 36. Example2 <ul><li>Exception handling in previous versions vs this Version </li></ul>
  37. 37. Example3: SQLXML <ul><li>SQLXML new interface </li></ul><ul><li>SQLXML SQL type </li></ul>
  38. 38. Example4 DatabaseMetaData <ul><li>What was available </li></ul><ul><li>What has been added </li></ul>
  39. 39. <ul><li>Introduction </li></ul><ul><li>New features and enhancements </li></ul><ul><li>Examples </li></ul><ul><li>Key points </li></ul><ul><li>Q/A </li></ul>Structure of this presentation
  40. 40. Key Points <ul><li>Programming ease and flexibility </li></ul><ul><ul><li>&quot;Getting Connected&quot; becomes easier </li></ul></ul><ul><ul><li>Using JDBC objects becomes more flexible </li></ul></ul><ul><ul><li>More API’s become available </li></ul></ul>
  41. 41. Key Points <ul><li>Automatic loading of JDBC Driver </li></ul><ul><li>SQL:2003 support including SQLXML </li></ul><ul><li>NCS support </li></ul><ul><li>Enhanced support for large objects </li></ul><ul><li>SQLException enhancement </li></ul>
  42. 42. <ul><li>Introduction </li></ul><ul><li>New features and enhancements </li></ul><ul><li>Examples </li></ul><ul><li>Key points </li></ul><ul><li>Q/A </li></ul>Structure of this presentation