rc101 010d JDBC - 0
rc101 010d JDBC - 0
rc101 010d JDBC - 0
www.datadirect.com/products/data-connectivity
Visit refcardz.com to browse and download the entire DZone Refcardz collection
#101
Get More Refcardz! Visit refcardz.com
CONTENTS INCLUDE:
n n
A Brief History JDBC Basics Driver Types and Architechture Performance Considerations Data Types Advanced JDBC and more...
A BRIEF HISTORY
Sun Microsystems created JDBC in the 90s to be the standard for data access on the Java Platform. JDBC has evolved since that time from a thin API on top of an ODBC driver to a fully featured data access standard whose capabilities have now surpassed its aging brother, ODBC. In recent applications, JDBC connects persistence layers (such as Hibernate or JPA) to relational data sources; but the JDBC API with its accompanying drivers are always the final piece connecting Java apps to their data! For more in depth (and entertaining) history, watch this movie on the history of Java and JDBC: http://www.youtube.com/watch?v=WAy9mgEYb6o
Hot Tip
The JDBC specification allows for fetching all data types using getString or getObject; however, it is a best practice to use the correct getXXX method as demonstrated in the code sample above to avoid unnecessary data conversions.
Executing a PreparedStatement
Use a PreparedStatement any time you have optional parameters to specify to the SQL Statement, or values that do not convert easily to strings, for example BLOBs. It also helps prevent SQL injection attacks when working with string values.
PreparedStatement pstmt = con.prepareStatement(INSERT into table2 (ID, lastName, firstName) VALUES (?,?,?)); pstmt.setInt(1, 87); pstmt.setString(2, Picard); pstmt.setString(3, Jean-Luc); rowsInserted += pstmt.executeUpdate();
JDBC BASICS
www.dzone.com
Connecting to a Server
Getting a basic Connection object from the database is the first operation to get a handle on. The code snippet below gets a connection to a SQL Server database. Note that the Class.forName line is unnecessary if you are using a JDBC 4.0 driver with Java SE 6 or above.
String url = jdbc:datadirect:sqlserver://nc-cqserver:1433;databaseNa me=testDB;user=test;password=test; try { Class.forName(com.ddtek.jdbc.sqlserver.SQLServerDriver); Connection con = DriverManager.getConnection(url); } catch (Exception except) { SQLException ex = new SQLException( Error Establishing Connection: + except.getMessage()); throw ex; }
Hot Tip
CallableStatements can return resultSets, even when inserting data on the server. If the application doesnt know if results should be returned, check for results by issuing a call to getMoreResults() after execution.
It is good to get metaData from the Connection object to see what driver and server version you are using. This comes in handy when its time to debug. Printing to system out or logging to a file is preferable:
DatabaseMetaData dbmd = con.getMetaData(); System.out.println( \nConnected with + dbmd.getDriverName() + + dbmd.getDriverVersion() + { + dbmd.getDriverMajorVersion() + , + dbmd.getDriverMinorVersion() + } + to + dbmd.getDatabaseProductName() + + dbmd.getDatabaseProductVersion() + \n);
Try a Type 5 driver today for: Oracle SQL Server DB2 Sybase MySQL Informix
Retrieving Data
A straightforward approach to retrieving data from a database is to simply select the data using a Statement object and iterate through the ResultSet object:
DZone, Inc.
|
Details at datadirect.com/products/jdbc
www.dzone.com
The JDBC-ODBC Bridge was the architecture that the first JDBC drivers adopted. This architecture requires an implementation of the JDBC API that then translates the incoming JDBC calls to the appropriate ODBC calls using the JNI (Java Native Interface). The requests are then sent to the underlying ODBC driver (which at the time was just a shell over the database native client libraries). The bridge implementation shipped with the JDK so you only needed the ODBC drivers and native DB client libraries to get started. Although this was a klunky and headache prone approach, it worked.
Type 3 drivers sought to be a 100% Java solution but never really gained much traction. Type 3 drivers had a Java client component and a Java server component, where the latter actually talked to the database. Although this was technically a full Java solution, the database vendors did not like this approach as it was costly they would have to rewrite their native client libraries which were all C/C++. In addition, this didnt increase the architectural efficiency as we are really still a 3 tier architecture so it is easy to see why this was never a popular choice.
The next generation of JDBC Drivers was the ever popular Type 2 driver architecture. This architecture eliminated the need for the ODBC driver and instead directly called the native client libraries shipped by the database vendors. This was quickly adopted by the DB vendors as it was quick and inexpensive to implement since they could reuse the existing C/ C++ based native libraries. This choice still left Java developers worrying about version and platform compatibility issues (i.e. client version 6 is not supported on HP-Itanium processors).
Hot Tip
Some vendors still do their new development in their native clients first. So, dont assume that if their website states that the JDBC driver supports Kerberos that they mean their Type 4 driver they may mean Type 2!
The most popular JDBC driver architecture to date is Type 4. This architecture encapsulates the entirety of the JDBC API implementation along with all the logic for communicating directly with the database in a single driver. This allows for easy deployment and streamlines the development process by having a single tier and a small driver all in a 100% java package.
Hot Tip
Type 4 drivers have been the traditional favorite of Java application developers since its inception due to the clean design and ease of use; drop in the driver jar and youre up and running!
TYPE 5: NEW!
While not yet officially sanctioned by the JDBC Expert Group, there is quite a bit of discussion surrounding the new Type 5 driver proposal in the JDBC community. Getting down to the real functional differences, we see this list as the requirements for Type 5 Drivers as follows:
Codeless Configuration The ability to modify options, check statistics and interact with the driver while it is running. Typically through a standard JMX MBean.
Drivers specifically designed for multi-core, 64 bit, and virtualized environments. Strict adherence to the JDBC standard, solving problems within the specification instead of using proprietary methods that promote vendor lock-in. Type 5 drivers unlock code that has been trapped in the vendor native client libraries and bring that into the Java community. Features include but are not limited to: Bulk Load, Client side High Availability, Kerberos, and others.
DZone, Inc.
www.dzone.com
PERFORMANCE CONSIDERATIONS
Hot Tip
Pooling objects results in significant performance savings. In JDBC, pooling Connection and Statement objects is the difference between a streamlined app and one that will consume all your memory. Make use of these pooling suggestions for all your JDBC applications!
how often you are committing transactions. Every commit causes the driver to send packet requests over the socket. Additionally, the database performs the actual commit which usually entails disk I/O on the server. Consider removing autocommit mode for your application and using manual commit instead to better control commit logic:
Connection.setAutoCommit(false);
Connection Pooling Enabling Connection pooling allows the pool manager to keep connections in a pool after they are closed. The next time a connection is needed, if the connection options requested match one in the pool then that connection is returned instead of incurring the overhead of establishing another actual socket connection to the server Statement Pooling Setting the MaxPooledStatements connection option enables statement pooling. Enabling statement pooling allows the driver to re-use PreparedStatement objects. When PreparedStatements are closed they are returned to the pool instead of being freed and the next PreparedStatement with the same SQL statement is retrieved from the pool rather than being instantiated and prepared against the server.
Hot Tip
Virtualization and Scalability are key factors to consider when choosing a JDBC driver. During the Performance Testing phase of your development cycle, ensure that your JDBC driver is using the least amount of CPU and Memory possible. You can get memory and CPU performance numbers from your driver vendor to see how the drivers will scale when deployed in a Cloud or other virtualized environment.
Hot Tip
Dont use PreparedStatements by default! If your SQL statement doesnt contain parameters use the Statement object instead this avoids a call to internal and wire level prepare() methods and increases performance!
MetaData Performance
Specify as many arguments to DatabaseMetaData methods as possible. This avoids unnecessary scans on the database. For example, dont call getTables like this:
ResultSet rs = dbmd.getTables(null,null,null,null);
Specifying at least the schema will avoid returning information on all tables for every schema when the request is sent to the server:
ResultSet rs = dbmd.getTables(null,test,null,null);
Most JDBC drivers populate the ResultSetMetaData object at fetch time because the needed data is returned in the server responses to the fetch request. Some underutilized pieces of ResultSetMetaData include:
ResultSetMetaData.getColumnCount() ResultSetMetaData.getColumnName() ResultSetMetaData.getColumnType() ResultSetMetaData.getColumnTypeName() ResultSetMetaData.getColumnDisplaySize() ResultSetMetaData.getPrecision() ResultSetMetaData.getScale()
Hot Tip
Instead of using getColumns to get data about a table, consider issuing a dummy query and using the returned ResultSetMetaData which avoids querying the system tables!
Commit Mode
When writing a JDBC application, make sure you consider
DZone, Inc.
|
www.dzone.com
WHATS IN A DRIVER?
To illustrate what a JDBC driver does under the covers, take a look at this anatomy of a JDBC driver diagram.
ADVANCED JDBC
These advanced features are complex and meant as an overview. For all the bells and whistles for these advanced options, check your JDBC driver documentation!
as well as importing/exporting new statements on the fly to fine tune application performance.
Hot Tip
Ensure that your data is secure by encrypting the wire traffic between the server and client using SSL encryption: (1) Set the EncryptionMethod connect option to SSL. (2) Specify the location and password of the trustStore file used for SSL server authentication. Set connect options or system properties (javax.net.ssl.trustStore and javax.net.ssl.trustStorePassword). (3) If your database server is configured for SSL client authentication, configure your keyStore information: (a) Specify the location and password of the keyStore file. Either set connect options or Java system properties (javax.net.ssl.keyStore and javax.net.ssl. keyStorePassword). (b) If any key entry in the keyStore file is passwordprotected, set the KeyPassword property to the key password.
and the KDC name for that realm. Alternatively, you can set the java.security.krb5.realm and java.security.krb5. kdc system properties. (3) If using Kerberos authentication with a Security Manager, grant security permissions to the application and driver.
SELECT (col1, col2, col3) FROM table1 WHERE col1 = foo SELECT (col1,col2,) FROM table_name ORDER BY column_name [ASC|DESC] SELECT column_name, aggregate_ function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name INSERT INTO table1 VALUES (val1, val2, value3,) INSERT INTO table2 (col1,col2,) VALUES (val1, val2, value3,) UPDATE table1 SET col1=val1, col2=val2, WHERE col3=some_val DELETE FROM table1 WHERE col2=some_val
Hot Tip
These security features are not supported by all databases and database versions. Check to ensure your database is setup appropriately before attempting Kerberos and SSL connections.
GROUP BY clause
Application Failover
Application failover is the ability for a driver to detect a connection failure and seamlessly reconnect you to an alternate server. Various types of failover exist for JDBC drivers so check your driver documentation for support - the most common are listed below:
Connection Failover Extended Failover In the case of the primary connection being unavailable, the connection will be established with the alternate server. While the application is running, if a connection failover occurs, the driver will reconnect to an alternate server and post a transaction failure to the application. Same as extended, except instead of posting a transaction failure, this level will reposition any ResultSets, so the application will not know there was a failure at all.
UPDATE statement
DELETE statement
Escape Clauses
Escape Type Call (a.k.a. stored procedure) Function Example
{call statement} {call getBookValues (?,?)} {fn functionCall} SELECT {fn UCASE(Name)} FROM Employee {oj outer-join} where outer-join is table-reference {LEFT | RIGHT | FULL} OUTER JOIN {table-reference | outer-join} ON search-condition SELECT Customers.CustID, Customers. Name, Orders.OrderID, Orders.Status FROM {oj Customers LEFT OUTER JOIN Orders ON Customers.CustID=Orders. CustID} WHERE Orders.Status=OPEN
Select Failover
Bulk Loading
Loading large amounts of data into a database quickly requires something more powerful than standard addBatch(). Database vendors offer a way to bulk load data, bypassing the normal wire protocol and normal insert procedure. There are 2 ways to use Bulk Loading with a JDBC driver that supports it: (1) Set enableBulkLoad connect option to true. This will make addBatch() calls use the bulk load protocol over the wire. (2) Use a Bulk Load object:
// Get Database Connection Connection con = DriverManager.getConnection(jdbc:datadirect:orac le://server3:1521;ServiceName=ORCL;User=test;Password=secret); // Get a DDBulkLoad object DDBulkLoad bulkLoad = DDBulkLoadFactory.getInstance(con); bulkLoad.setTableName(GBMAXTABLE); bulkLoad.load(tmp.csv); // Alternatively, you can load from any ResultSet object into the target table: bulkLoad.load(results);
Outer Join
Date Escape
{d yyy-mm-dd} UPDATE Orders SET OpenDate={d 200501-31} WHERE OrderID=1025 {t hh:mm:ss} UPDATE Orders SET OrderTime={t 12:30:45} WHERE OrderID=1025 {ts yyyy-mm-dd hh:mm:ss[.f...]} UPDATE Orders SET shipTS={ts 2005-0205 12:30:45} WHERE OrderID=1025
Time Escape
TimeStamp Escape
Hot Tip
WildCard % (percent)
To get a listing of the functions supported by a given JDBC driver, use the getter methods on the DatabaseMetaData object: getStringFunctions(), getNumericFunctions(), getTimeDateFunctions(), etc.
Description and Example Subsititute for zero or more characters.
SELECT * from emp where name like Da%
Hot Tip
For additional Bulk Load options, check the JDBC driver documentation.
_ (underscore) [charlist]
[!charlist] -or[^charlist]
DZone, Inc.
www.dzone.com
Hot Tip
When choosing a driver to use with Hibernate, ensure your driver supports Codeless Configuration so that you can tune performance and change driver behavior without having to modify the Hibernate code!
When writing Hibernate applications it is important to understand the main files used to setup a Hibernate environment:
Hibernate File Dialects (org.hibernate.dialect.*) Purpose Describes the SQL behavior of the JDBC driver and database to which the application is connecting. Contains the hibernate configuration settings, such as: JDBC driver and connection information, dialect information, mapping information, etc. The mapping file contains the mapping between the application defined objects and the relational data stored in the database.
Mapping File
ABOUT THE AUTHOR
Jesse Davis watched his Dad code on his Apple IIC Plus,
and his addiction to technology began. He used his first PC (a Packard Bell) in high school to run Slackware Linux and began writing shell scripts and simple C applications. Honing his skills as a Computer Engineer at North Carolina State University, Jesse loved the challenge of combining hardware and software and concentrated on microprocessor architecture and design graduating with honors in Y2K. Today, he enjoys teaching others about the latest technological breakthroughs and enjoys building robots and woodworking projects with his kids. During the day, he is the Senior Engineering Manager for the Progress|DataDirect Connect product line, and has more than 12 years of experience developing database middleware, including JDBC and ODBC drivers, ADO.NET providers, and data services. Jesse is responsible for product development initiatives and forward looking research, and is an active member of the JDBC Expert Group, working on the next version of JDBC.
Hot Tip
Not all JDBC drivers are created equal! Look for a set of JDBC drivers that can use a single dialect to connect to multiple versions of a database. Theres nothing worse than deploying your application with an Oracle 8 dialect and discover that you need to redeploy with an Oracle 10 dialect!
RECOMMENDED BOOK
Performance and scalability are more critical than ever in todays enterprise database applications, and traditional database tuning isnt nearly enough to solve the performance problems you are likely to see in those applications. Nowadays, 75-95% of the time it takes to process a data request is typically spent in the database middleware. Todays worst performance and scalability problems are generally caused by issues with networking, database drivers, the broader software/hardware environment, and inefficient coding of data requests. In The Data Access Handbook, two of the worlds leading experts on database access systematically address these issues, showing how to achieve remarkable improvements in performance of realworld database applications.
BUY NOW
books.dzone.com/books/data-access-handbook
#82
Get More Refcardz! Visit refcardz.com
CONTENTS INCLUDE:
Cost by... Data Tier Technologies t to you ply. brough Platform Management and more... te. Com bora
Aldon
Chan ge. Colla
#6 Cloud 4Computing
By Daniel Rubio
also minimizes the need to make design changes to support CON TEN TS one time events. INC
ref ca
Cloud Computing
active Repo are within units This Refcard will introduce to you to clouddcomputing, with an softw riente loping ine task-o e Deve Mainl es you emphasis oncodelines providers, so by Commit better understand these softwar chang Level can e code ding Task what it is a cloud computingaplatform can offer your web trol ut line Policy nize sourc es as of buil NT Orga Code e witho it chang cess ion con e name sourc T CO applications. and subm it the pro jects vers are from with uniqu ABOU softw (CI) is um the build evel Comm build a pro minim Label Task-L ies to gration ed to activit blem the bare ion ate all cies to t ous Inte committ USAGE SCENARIOS to a pro Autom congurat nden ymen Build depe al tion deplo t Continu ry change ive manu Label d tool nmen the same stalle , a solu , ineffect ) Build eve t, use target enviro ated s (i.e. ce pre-in blem with ymen (i.e. Redu Autom ory. d deploEAR) each lar pro pattern tterns Pay only what you consume in tagge or cies reposit particu tions that e ed via t nden anti-pa For each (e.g. WAR es al Depe ge t a few explain text) and to x the are solu duc Web application deployment untillibrari yearsenvironmen similar t ago was packa Minim be nden ns to pro all targe rity all depe used CI can ticular con le that alize y i-patter they tend es, but can toInteg late Centr Binar most phone services: plans with alloted resources, with an ts etimes , temp s. Ant nt nmen ctic in a par hes som geme t enviro e a single based on incurred cost whether such resources were consumedto thenot. or proces , in the end bad pra enting Mana targe Creat cy nt es rties are rily nden implem approac ed with the cial, but chang prope Depe into differe itting er necessa pared to e te builds e comm late Veri associat to be ben befor are not Cloud computing asRun remo its known etoday has changed this. etc. Temp n com Build ually, They Privat contin lts whe appear effects. rm a The various resources consumed by webperiodically, applications (e.g.nt team Perfo opme ed resu tion d Builds sitory Build to devel Repo Stage adverse unintend Integra bandwidth, memory, CPU) areIntegration on from CI server basis tallied a per-unit e e Build rm an tinuous ack ard produc Privat Con feedb Refc on (starting from zero) by Perfomajor cloud computing platforms. all tern. ated occur based tion autom as they the term cycle, this the pat builds h as Build Send soon ration Integra al use of ion with ts suc ors as Integ entat and test concep tinuous vention docum oper Con build include the con s to the rate devel Gene CI to While efer ion of the not s on expand
Vis it
Ge t
Mo re
w ww.dzone.com
S INUOU
INTEG
RATION
Re fca
rd z!
e.com
Ge t Mo re
E: INC LUD gration NTS ous Inte Change CO NTE Continu at Every ns About Software i-patter Build and Ant Patterns Control ment Version e... Manage s and mor Build tice d Prac Buil
to isolat space n e Work riptio itory a Desc These companies have Privat deployed webmanage applications are in long trol repos to n-con lop softw ing and Deve a versio that adapt and scale to large user bases, making them rn les to ize merg Patte it all to minim space Comm many aspects related tomultiple computing. s cloud e Work knowledgeable in a mainline Privat that utilize lop on Deve code lines a system sitory of work
rdz .co
Re fca
rdz !
However, the demands and technology used on such servers has changed substantially in recent years, especially with the entrance of service providers like Amazon, Google and es Microsoft. e chang
Elem al Elem ents ents and Large scale growth scenarios involving specialized equipment more... (e.g. load balancers and clusters) are all but abstracted away by
Vis it
#84
dz. com
ref car
Web applications have always been deployed on servers connected to what is now deemed the cloud.
ation Having the capability to support one time events, cloud Useful Open computing platforms also facilitate the gradual growth curves Page Source Stru Tools faced by web applications. cture Key
HTML
Basics
LUD E:
Valid
ML
Structur
ICS In addition, several cloud computing platforms support data HTM tier technologies that L and the precedent set by Relational exceed XHT HTML Database Systems (RDBMS): Map Reduce, web service APIs, is used ML are the prog etc. Some platforms rams writ large scale RDBMS deployments. support as the grap foundation The src of all hical and Java ten attribute and the also recein JavaScri user interfac web develop desc as the e in clien ment. ive data pt. Server-s the ima alt attribute ribes whe output likew ge is describe re the ima mechan from CLOUD COMPUTING PLATFORMS AND ide languag t-side ise unavaila ge le ism. The web pag s alte Nested es like was onc use HTML ble. rnate can be emergin es and use UNDERLYING CONCEPTS and XHT PHP text that e tags found, Tags standard a very loos g Ajax HTML ML as is disp can tech ely-de thei ization, layed cannot be (and freq need ned lang r visual eng nologies if but as for overlap uen it has ine. b></ Amazon EC2: ther stanstandard software and uage with whe Industry dard , so <a>< tly are) nest become virtualization HTM a> is s has ne. b></ ed insid bec heavily based on very little L more Amazons cloud computing ose the curr you cho platform isome a></ importa e b> is more ent stan to writ not lega each othe app nt, that will industry standard software and virtualization technology. e HTM dards HTM r. Tags l, but L or XHT arent. Reg the will help L VS <a>< and XHT simplify all ardless XHTM b></ ML, you ML physical r othe you prov to L Virtualization allows aare actu piece of hardware idebe understand of HTML much a solid of the ing has ally simp r web cod utilized by multiple operating systems. This allows resources function job adm been arou ler than ing. Fort foundation commo nd for unately they (e.g. bandwidth,n elem CPU) to be mov memory, ality has allocated exclusively to expecte irably, that some used Every job time. d. Earl to be, HTML ent instances. ed to CSS pag Brow individual operating system s Whi y HTM has expand because . ser common e (HTML ed far le it has don or XHT web dev manufacture L had very more e its extensio .) All are ML shar limit than rs add elopers As a user of Amazons EC2 cloud computing platform, you are result anybody es cert ed man ed layout n. HTM essentia came is proc lly plai supp L les assigned essor an operating system in the same wayain on all hosting as elements ort. The late a lack of stan up with clev y competi n text should ng stan st web in dar er wor not be dar kar standar cr
HTM
L BAS
50795
DZone communities deliver over 6 million pages each month to more than 3.3 million software developers, architects and decision makers. DZone offers something for everyone, including news, tutorials, cheatsheets, blogs, feature articles, source code and more. DZone is a developers dream, says PC Magazine.
9 781934 238714
Version 1.0
Copyright 2010 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.