Java Database Connectivity - JDBC Notes
Java Database Connectivity - JDBC Notes
2. Load driver
7. Process results
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
3 JDBC 4
For Oracle
Class.forName(“oracle.jdbc.driver.OracleDriver ");
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
5 JDBC 6 JDBC
page 1
con = DriverManager.getConnection(conURL,usr,pswd);
• String conURL = “jdbc:odbc:personDSN”
7 JDBC T.A: Fahad Shakeel & Hamzah Shabbir © 2009 8 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
9 JDBC 10 JDBC
page 2
JDBC
• Statement st = con.createStatement();
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
13 JDBC 14 JDBC
JDBC Architecture
1 3 6
creates creates creates
Driver Manager Connection Statement ResultSet
SQL 4 Data 5
2
Establish Link
Driver
Example Code
To DB
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
15 JDBC 16
17 JDBC T.A: Fahad Shakeel & Hamzah Shabbir © 2009 18 JDBC T.A: Fahad Shakeel & Hamzah Shabbir © 2009
page 3
JDBC
} //end main
}//end class
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
19 JDBC 20 JDBC
• executeQuery
– Executes the SQL query and returns the data in a table
(ResultSet)
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
21 22 JDBC
Connection con=null;
int num = stmt.executeUpdate("DELETE FROM Person " + con = DriverManager.getConnection(url, usr, pwd);
"WHERE id = 2");
Statement st = con.createStatement();
23 JDBC T.A: Fahad Shakeel & Hamzah Shabbir © 2009 24 JDBC T.A: Fahad Shakeel & Hamzah Shabbir © 2009
page 4
JDBC
} //end main
//Step 7: Process the results of the query
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
25 JDBC 26 JDBC
• getQueryTimeout( ) / setQueryTimeout(int)
– Specifies the amount of a time (seconds) a driver will
wait for a STATEMENT to complete before throwing a
SQLException
After execution
page 5
JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
31 JDBC 32 JDBC
Example Code
public static void main (String args [ ]){
try {
Using Prepared Statements //steps 2 to 4
Class.forName(“driver name”);
Connection con=null;
con = DriverManager.getConnection(url, usr, pwd);
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
33 34 JDBC
35 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 36 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009
page 6
JDBC
Before execution
Result Set
After execution
37 JDBC 38
T.A: Fahad Shakeel & Hamzah Shabbir © 2009
ResultSet
• Overview Row numbers
ResultSet
– A ResultSet contains the results of the SQL query 0 id Name Address phoneNum
• Represented by a table with rows and columns 1 1 ali model town 9203256
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
39 JDBC 40 JDBC
String sql = “SELECT * FROM Person”; • The first call to next, moves the cursor to the first row
PreparedStatement pStmt = con.prepareStatement(sql);
– close( )
ResultSet rs = pStmt.executeQuery( );
• Releases the JDBC and database resources
• The result set is automatically closed when the
associated Statement object executes a new query or
closed by method call
41 JDBC T.A: Fahad Shakeel & Hamzah Shabbir © 2009 42 JDBC T.A: Fahad Shakeel & Hamzah Shabbir © 2009
page 7
JDBC
43 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 44 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009
• Moves the cursor to the previous row in the public static void main ( String args[ ]) {
ResultSet object.
try {
• Returns true if cursor is on a valid row, false it // load driver & make connection
is off the result set.
String sql = “SELECT * FROM Person”;
• Throws exception if result type is TYPE_FORWARD_ONLY.
PreparedStatement pStmt = con.prepareStatement( sql,
ResultSet.TYPE_SCROLL_INSENSITIVE ,
ResultSet.CONCUR_UPDATABlE );
ResultSet rs = pStmt.executeQuery( );
45 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 46 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009
rs.next(); }
47 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 48 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009
page 8
JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
49 JDBC 50 JDBC
page 9
JDBC
• Insert row – a buffer, where a new row may be • The cursor must be on the insert row when this
construted by calling the updater methods method is called
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
55 JDBC 56 JDBC
page 10
JDBC
• Throws exception if the result set is TYPE_FORWARD_ONLY • Throws exception when the cursor is on the insert row
– getRow( )
• Returns the current row numner
• The first row number is 1, second row number is 2 and
so on
61 JDBC T.A: Fahad Shakeel & Hamzah Shabbir © 2009 62 JDBC T.A: Fahad Shakeel & Hamzah Shabbir © 2009
Meta Data
Meta Data
65
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 66 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009
page 11
JDBC
67 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 68 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009
page 12
JDBC
con.close();
73 JDBC T.A: Fahad Shakeel & Hamzah Shabbir © 2009 74 JDBC T.A: Fahad Shakeel & Hamzah Shabbir © 2009
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
75 JDBC 76 JDBC
• isReadOnly ( )
– Retrieves whether this database is in read-only mode.
– Returns true if so, false otherwise.
77 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 78 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009
page 13
JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
79 JDBC 80 JDBC
// process results
con.close();
RowSet
• A JDBC RowSet object holds tabular data in a way
that makes it more flexible and easier to use than a
result set.
page 14
JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
85 JDBC 86 JDBC
RowSet (cont.)
2. CachedRowSet
– Disconnected RowSet that is scrollable and updateable.
– It caches the data of a ResultSet in memory.
– Manipulate data and make changes to data while it is JDBC Drivers
disconnected.
Types
– Reconnect to the data source to write changes back to it.
– It is also serializable, so it can be sent across a network.
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
87 JDBC 88
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
89 JDBC 90 JDBC
page 15
JDBC
91 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 92 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
95 JDBC 96 JDBC
page 16
JDBC
Type 3 Type 4
97 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 98 JDBC
T.A: Fahad Shakeel & Hamzah Shabbir © 2009
String name;
String sal;
• Connection, Statement, PreparedStatement,
ResultSet and RowSet are all interfaces. // constructor
// getter / setters
Database connectivity & business logic
void insertEmp( ){ all in one class
• Why? // connect database
// execute query
// process results
– Any DBMS interested in providing support for java }
connectivity, need to provide implementation of all the void retrieveEmp ( ){ ….. }
above interfaces.
void calculateTax ( ) { …. }
}
T.A: Fahad Shakeel & Hamzah Shabbir © 2009 T.A: Fahad Shakeel & Hamzah Shabbir © 2009
99 JDBC 100 JDBC
page 17
JDBC
On-line Resources
• Sun’s JDBC Site
– http://java.sun.com/products/jdbc/
• JDBC Tutorial
– http://java.sun.com/docs/books/tutorial/jdbc/
page 18