steps
import the required packages
register the jdbc drivers
Class.forName("com.mysql.jdbc.Driver");
open a connection to a database
DriverManager.getConnection();
create a statement object
execute a query and return a ResultSet object
process the ResultSet object
close the ResultSet and Statement Objcet
close the connnection
architecture
jdbc
pic
core
performance
select the right JDBC driver
simplify sql queries
minimizing the use of database metadata methods
manage database connection objects
setting optimal connection properties
setting an optimal row prefetch value
using a connection pool
example
connect once and using multiple statement objects
controlling a transaction
many statement object executed as a single unit
choosing the optimal transaction isolation level
transaction level
TRANSACTION_NONE-0
TRANSACTION_READ_UNCOMMITTED-1
TRANSACTION_READ_COMMITTED-2
TRANSACTION_REPEATABLE_READ-4
TRANSACTION_SERIALIZABLE-8
avoding using generic search patterns
example
retrieving only required data
return the only columns you need
set driver specific methods
Statement.setMaxRows
Statement.setMaxFieldSize
ResultSet.setFetchSize
Statement.setFetchSize
Statement or PrepareStatement
Statement
PrepareStatement
http://www.javaeye.com/topic/49129?page=3
choose the right cursor
ResultSet.TYPE_FORWARD_ONLY
ResultSet.TYPE_SCROLL_INSENSITIVE
ResultSet.TYPE_SCROLL_SENTITIVE
caching PreparedStatement Objects
PreparedStatement object pooling to cache rarely changed sql or queries
tiers
2 tiers
pic
3 tiers
pic
API
java.sql
elements
DriverManager
driver
jdbc-odbc
pic
not recommended
experimental
not as fast as jdbc
do not support multiple concurrent open statements
not support Blob and Clob
install
how to load
Class.forName("com.mysql.jdbc.Driver");
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
instance a Driver class
use System.properties()
java -Djdbc.drivers=com.mysql.jdbc.Driver:sun.jdbc.odbc.JdbcOdbcDriver Test
use a thread class
type
JDBC-ODBC
some ODBC native code must be loaded on each client machine
pic
native-API
requires some binary code be loaded on each client machine
pic
network-protocol
translate JDBC API calls into a DBMS-independent net protocol
pic
native-protocol
convert JDBC calls into the network protocol used by DBMS directly
pic
select criteria
features and ease of use
speed/performance
reliability
security
protability
support
price
open source
compatibility
Connection
URL
jdbc:mysql://localhost/db1
pic
Class.forName()
unicode
datasource
JNDI
PIC
benefits
properties
pic
create DataSource Object
Statement
execute()
executeBatch()
executeQuery()
executeUpdate()
PreparedStatement
example
CallableStatement
ResultSet
get index start from 1
get table row count
SQLExcepiton
javax.sql
DataSource to establish a connection
Connection pooling
benefits
limited connections
performance
interface
ConnectionPoolDataSource
PooledConnection
products
Apache Avalon/Excalibur
Commons Pool
commons DBCP
Distributed transactions
Row sets
metadata
DatabaseMetaData
ResultSetMetaData
ParameterMetaData
RowSetMetaData
transaction
default autocommit is true
sql statements are executed and committed as individual transaction
for every single statement, a transaction begins-->execute the statement successfully-->commit
conn.setAutoCommit(false);
rollback
any point before committing a transaction
conn.rollback();
how to start
explicit
then set autocommit back to true
debug
DriverManager.setLogWriter(pw);
special
data type
Blob
Clob
Date/Time/Timestamp