Académique Documents
Professionnel Documents
Culture Documents
Hunter
Chapter 18
JDBC
Background:
databases
Background: databases
Types of databases
--Hierarchical
--Relational
--Object Relational
Background: databases
Types of databases
--Hierarchical-the first database,
invented by IBM, called IMS.
Background: databases
Types of databases
--Relational-the most common, invented
by IBM but first marketed by Oracle.
Examples:
Oracle
DB2
Sybase
Access*
Background: databases
Types of databases
Relational Databases
Here is a sample
table, USER
ID
LastName
Primary Keymust
uniquely identify a row.
No nulls allowed.
FirstName
Age
Jones
Sam
32
Jones
Angela
27
Smith
Ann
22
Doe
Jack
44
Code
Message
Bill Paid
Bill Overdue
Account closed
Smith
Smith
Doe
Doe
FirstName
FirstName
Sam
Sam
Age
Age
32
32
Code
Angela
Angela
Ann
Ann
27
27
22
22
Jack
Jack
44
44
SQL Basics
JDBC Basics:
Connection
= jdbc:oracle:thin:@myhostname:1521:OurDB
This line
Connection con = DriverManager.getConnection(
url,substitutes
user, pass );494
while( rs.next() )
{
String myCol1 = rs.getString( COL1 );
String myCol2 = rs.getString( COL2 );
}
These correspond to
columns in the original
query.
Java II--Copyright 2001-2004 Tom
Hunter
try
{
String output = null;
String query = SELECT username from MYTABLE where pass=foo ;
Connection con = DriverManager.getConnection( url, us, pass);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery( query );
while( rs.next() )
{
output = rs.getString( username );
}
rs.close();
stmt.close();
con.close();
}
catch( SQLException sql )
{
System.out.println( Uh oh );
}
DataSource
DataSource
As I said, the DriverManager is not the best
choice for a production system. It doesnt scale
well.
A better alternative is using a DataSource.
A DataSource offers connection pooling,
pooling
where new connections are not thrown away
but are instead set aside for the next time
someone needs a connection.
assigned to the
DataSource when
I created it. Here,
Im just looking it
up under the name
I stored it
DataSource:Complexity of setup
Using a DataSource is very valuable because
it allows connection pooling.
The downside of using a DataSource is the
complexity of its setup. Also, each Application
Server vendor has its own unique setup. You
will need to learn these*.