Professional Documents
Culture Documents
Islamabad, Pakistan
JAVA
FALL 2009
Instructor/TA
Lecture
Agenda
JAVA @ FAST
PART I: OVERVIEW OF
DATABASES AND JAVA
FALL 2009
SQL
JAVA @ FAST
SQL Syntax
INSERT INTO table ( field1, field2 )
VALUES ( value1, value2 )
inserts a new record into the named table
UPDATE table SET ( field1 = value1,
field2 = value2 ) WHERE condition
changes an existing record or records
DELETE FROM table WHERE condition
removes all records that match condition
SELECT field1, field2 FROM table WHERE
condition
retrieves all records that match condition
6
JAVA @ FAST
Transactions
JAVA @ FAST
FALL 2009
JDBC Architecture
Application
JDBC
Driver
JAVA @ FAST
JDBC Drivers
Type I: Bridge
Type II: Native
Type III: Middleware
Type IV: Pure
10
JAVA @ FAST
ODBC
Driver
ODBC
CLI (.lib)
JDBC
Middleware
Server
Type III
Middleware
Type IV
Pure
11
JAVA @ FAST
Type I Drivers
12
JAVA @ FAST
Type II Drivers
13
JAVA @ FAST
14
JAVA @ FAST
Type IV Drivers
15
JAVA @ FAST
JDBC Limitations
No scrolling cursors
No bookmarks
16
JAVA @ FAST
FALL 2009
java.sql
18
JAVA @ FAST
DriverManager
19
JAVA @ FAST
20
JAVA @ FAST
Example
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
DriverManager.getConnection(url,username,passwor
d);
Calling the Class.forName automatically creates an instance of a
driver and registers it with the DriverManager, so you don't need to
create an instance of the class. If you were to create your own
instance, you would be creating an unnecessary duplicate, but it
would do no harm.
String url="jdbc:odbc:test1";
21
JAVA @ FAST
DriverManager
Driver
Statement/Prepared Statement
Connection
ResultSet
22
JAVA @ FAST
Driver
Connection
Statement
ResultSet
23
JAVA @ FAST
JDBC URLs
jdbc:subprotocol:source
jdbc:odbc:DataSource
e.g. jdbc:odbc:Northwind
jdbc:msql://host[:port]/database
e.g. jdbc:msql://foo.nowhere.com:4333/accounting
24
JAVA @ FAST
DriverManager
Connection getConnection
(String url, String user, String
password)
Connects to given JDBC URL with given user name
and password
Throws java.sql.SQLException
returns a Connection object
25
JAVA @ FAST
Connection
JAVA @ FAST
Obtaining a Connection
String url
= "jdbc:odbc:Northwind";
try {
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url);
}
catch (ClassNotFoundException e)
{ e.printStackTrace(); }
catch (SQLException e)
{ e.printStackTrace(); }
27
JAVA @ FAST
Make a Statement
Statement createStatement()
PreparedStatement prepareStatement(String
sql)
28
JAVA @ FAST
Statement
29
JAVA @ FAST
Execute a Query
ResultSet executeQuery(String)
int executeUpdate(String)
boolean execute(String)
JAVA @ FAST
ResultSet
JAVA @ FAST
ResultSet Methods
boolean next()
activates the next row
the first call to next() activates the first row
returns false if there are no more rows
void close()
disposes of the ResultSet
allows you to re-use the Statement that created it
automatically called by most Statement methods
32
JAVA @ FAST
ResultSet Methods
33
JAVA @ FAST
ResultSet Methods
JAVA @ FAST
ResultSet Methods
JAVA @ FAST
isNull
36
JAVA @ FAST
Sample Database
Employee ID
Last Name
First Name
1
2
3
4
Davolio
Fuller
Leverling
Peacock
Nancy
Andrew
Janet
Margaret
Buchanan
Steven
37
JAVA @ FAST
SELECT Example
Connection con =
DriverManager.getConnection(url, "alex",
"8675309");
Statement st = con.createStatement();
ResultSet results = st.executeQuery("SELECT
EmployeeID, LastName, FirstName FROM
Employees");
38
JAVA @ FAST
39
JAVA @ FAST
Java Type
String
java.math.BigDecimal
boolean
byte
short
int
long
float
double
byte[]
java.sql.Date
java.sql.Time
java.sql.Timestamp
40
JAVA @ FAST
Database Time
java.sql.Time
java.sql.Timestamp
year, month, day, hours, minutes, seconds, nanoseconds
usually use this one
41
JAVA @ FAST
42
JAVA @ FAST
INSERT example
43
JAVA @ FAST
Transaction Management
44
JAVA @ FAST
setAutoCommit
Connection.setAutoCommit(boolean)
if AutoCommit is false, then every statement is
added to an ongoing transaction
you must explicitly commit or rollback the
transaction using Connection.commit() and
Connection.rollback()
45
JAVA @ FAST
Whoa!
46
JAVA @ FAST
ResultSetMetaData
What's the suggested column title for use in printouts and displays?
and so on...
47
JAVA @ FAST
48
JAVA @ FAST