Professional Documents
Culture Documents
4280 rc101 010d JDBC - 0 PDF
4280 rc101 010d JDBC - 0 PDF
32-bit
64-bit
Windows
UNIX
Linux
More
www.datadirect.com/products/data-connectivity
Visit refcardz.com to browse and download the entire DZone Refcardz collection
#101
CONTENTS INCLUDE:
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:
Hot
Tip
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.
http://www.youtube.com/watch?v=WAy9mgEYb6o
www.dzone.com
JDBC BASICS
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.
Hot
Tip
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.
Sybase
MySQL
Informix
Details at datadirect.com/products/jdbc
www.dzone.com
Hot
Tip
Hot
Tip
TYPE 5: NEW!
DZone, Inc.
Performance
Architecture
Clean Spec
Implementation
Advanced
Functionality
www.dzone.com
PERFORMANCE CONSIDERATIONS
Hot
Tip
Connection.setAutoCommit(false);
Hot
Tip
Technique
Benefit
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);
Hot
Tip
Hot
Tip
Commit Mode
When writing a JDBC application, make sure you consider
DZone, Inc.
JDBC Types
Java Type
CHAR, VARCHAR,
LONGVARCHAR
java.lang.String
CLOB
java.sql.Clob
NUMERIC, DECIMAL
java.math.BigDecimal
BIT, BOOLEAN
Boolean
BINARY, VARBINARY,
LONGVARBINARY
byte[]
BLOB
java.sql.Blob
DATE
java.sql.Date
TIME
java.sql.Time
TIMESTAMP
java.sql.Timestamp
TINYINT
byte
SMALLINT
short
INTEGER
int
BIGINT
long
REAL
float
FLOAT, DOUBLE
double
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
Hot
Tip
Class.forName(com.ddtek.jdbc.sqlserver.SQLServerDriver);
(b) If any key entry in the keyStore file is passwordprotected, set the KeyPassword property to the
key password.
www.dzone.com
Hot
Tip
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
WHERE clause
ORDER BY clause
SELECT (col1,col2,)
FROM table_name
ORDER BY column_name [ASC|DESC]
GROUP BY clause
INSERT statement
(all columns implicit)
(explicit columns)
UPDATE statement
UPDATE table1
SET col1=val1, col2=val2,
WHERE col3=some_val
DELETE statement
Escape Clauses
Select Failover
Bulk Loading
Escape Type
Example
{call statement}
{call getBookValues (?,?)}
Function
{fn functionCall}
SELECT {fn UCASE(Name)} FROM Employee
Outer Join
{oj outer-join}
where outer-join is
table-reference {LEFT | RIGHT | FULL}
OUTER JOIN
{table-reference | outer-join} ON
search-condition
Date Escape
{d yyy-mm-dd}
UPDATE Orders SET OpenDate={d 200501-31} WHERE OrderID=1025
Time Escape
{t hh:mm:ss}
UPDATE Orders SET OrderTime={t
12:30:45} WHERE OrderID=1025
TimeStamp Escape
Hot
Tip
bulkLoad.load(results);
Hot
Tip
WildCard
% (percent)
_ (underscore)
[charlist]
SQL Construct
Example
SELECT statement
DZone, Inc.
[!charlist]
-or[^charlist]
www.dzone.com
Hot
Tip
Purpose
Dialects (org.hibernate.dialect.*)
Mapping File
Hot
Tip
ABOUT THE AUTHOR
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.
Blog: http://blogs.datadirect.com/
dz. com
HTML
LUD E:
Basics
ref car
L vs XHT technologies
Automated growthHTM
& scalable
to isolat
space
n
e Work
riptio
itory
a Privat
Desc
ge
These companies
have
webmana
applications
are in long deployed
trol repos
to
n-con
lop softw
ing and making them
Deve
a versio
that adapt
and scale
bases,
rn
les toto large user
ize merg
Patte
it all fi
minim
le
space
Comm
ine to
tos multip
cloud
computing.
e Work knowledgeable in amany
mainl aspects related
Privat
that utilize
lop on
Deve code lines
a system
sitory
of work
Valid
ML
Vis it
Apache Ant
Hadoop
Spring Security
Subversion
Elem
al Elem ents
Large scale growth scenarios involvingents
specialized
and mor equipment
e... away by
(e.g. load balancers and clusters) are all but abstracted
Structur
rdz !
e.com
RATION
Upcoming Refcardz
By Daniel Rubio
ge. Colla
Chan
HTM
L BAS
Core
HTML
By An
dy Ha
rris
Cloud Computing
Vis it
INTEG
Cloud#64Computing
active
Repo
are within
d units
This Refcard
will introduce
to you to cloud
softw
riente computing, with an
loping
ine
task-o it
e
Mainl
es by
emphasis onDeve
these
so
youComm
can better understand
ines providers,
softwar
chang
codel
e code
ding
Task Level
sourc es as aplatform
what it is a cloudnize
computing
can offer your
trol
ut web
line Policy
of buil
NT
Orga
Code
e witho
it chang
cess
ion con
e name
sourc
T CO
and subm
applications.
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 configurat
nden
ymen
Build
depe
al
deplo
t
Continu ry change
manu
Label
d tool
solution fective
nmen
a
the same
stalle
,
)
Build
eve
t, use target enviro
(i.e.
, inef
ated
ce pre-in
with
ymen
Redu
problem
Autom
terns
ns (i.e.
ory.
d deploEAR) in each
Pay only
consume
ticular
tagge
via pat
or
cieswhat you
-patter
s that
reposit
t
nden
For each (e.g. WAR
es
lained ) and anti
the par solution duce
al Depeapplication deployment
ge
librari
x
t
exp
Web
until
a
few
years
agonmen
was similar
t enviro
packa
Minim
are
be
nden
text
to fi
to pro
all targe
rity
all depe
tterns
can
used
CI can ticular con
le that
alizeplans with
tend
y Integ
to
most phone services:
late fialloted resources,
Centr
Binar
ts with an
Anti-pa
they
es, but
etimes
temp
nt
nmen
on
ng
end,
in a par hes som
cess.
geme
practic
t enviro
e a single
incurred
cost
whether
such
resources
were
consumed
or
pro
enti
the
based
thenot.
Mana
targe
in
Creat
cy
nt
es to
rties are
rily bad
nden
implem
approac ed with the cial, but,
chang
prope
Depe
into differe
itting
er
necessa pared to
efi
te builds
e comm
late Verifi
associat to be ben
befor
etc.
are not
Cloud
computing asRun
itsremo
known etoday
has changed this.
Temp
n com
Build
ually,
They
Privat
lts whe
y, contin
appear effects.
rm a
nt team
dicall
The various
resourcesPerfo
consumed
by webperio
applications
(e.g.
opme
ed resu
d Builds
sitory
Build
gration
r to devel
Repo
Stage
adverse unintend
ration on a per-unit
bandwidth,
memory, CPU)
areInteg
tallied
basis
CI serve
e
ous Inte
from
e Build
rm an
tinu
ack
ard
produc
Privat
Con
feedb computing platforms.
Refc
on
(starting
from zero) by Perfo
all majorated
cloud
tern.
occur
term
based
autom
e, this
as
as they
the pat
builds
Build
gration
Send
of the
soon
cycl
such
ration
ion with
ors as
Integ
entat
ous Inte tional use and test concepts
tinu
docum
ven
oper
Con
build include
the con s to the
rate devel
to
Gene
While
of CI
efer
notion
the
s on
expand
S
INUOU
rd z!
BUY NOW
DZone, Inc.
140 Preston Executive Dr.
Suite 100
Cary, NC 27513
ISBN-13: 978-1-934238-71-4
ISBN-10: 1-934238-71-6
50795
888.678.0399
919.678.0300
Refcardz Feedback Welcome
refcardz@dzone.com
Sponsorship Opportunities
sales@dzone.com
$7.95
ref ca
Re fca
Aldon
Re fca
Mo re
Ge t Mo
re
Ge t
tion:
tegra ternvasll
us Ind Anti-PPaat
Du
ul M.
nuorn
By
an
ti
s
n
o
C
Patte
w ww.dzone.com
rdz .co
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
Practice
d
Buil
#84
CONTENTS INCLUDE:
books.dzone.com/books/data-access-handbook
Twitter: @jldavis007
Cost
by...
youTechnologies
Data
t toTier
Comply.
brough
Platform Management and more...
borate.
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
Jesse Davis watched his Dad code on his Apple IIC Plus,
#82
9 781934 238714
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.
Version 1.0