Basic Steps in Using JDBC
Basic Steps in Using JDBC
JDBC
2. Load driver
7. Process results
For Oracle
Class.forName(“oracle.jdbc.driver.OracleDriver ");
con = DriverManager.getConnection(conURL,usr,pswd);
• String conURL = “jdbc:odbc:personDSN”
7 JDBC Umair Javed©2005 8 JDBC Umair Javed©2005
• Statement st = con.createStatement();
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
} //end main
}//end class
• executeQuery
– Executes the SQL query and returns the data in a table
(ResultSet)
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 Umair Javed©2005 24 JDBC Umair Javed©2005
} //end main
//Step 7: Process the results of the query
• 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
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);
Before execution
Result Set
After execution
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
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 Umair Javed©2005 42 JDBC Umair Javed©2005
• 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 Umair Javed©2005 46 JDBC Umair Javed©2005
rs.next(); }
• 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
• 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 Umair Javed©2005 62 JDBC Umair Javed©2005
Meta Data
Meta Data
con.close();
• isReadOnly ( )
– Retrieves whether this database is in read-only mode.
– Returns true if so, false otherwise.
// 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.
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.
Type 3 Type 4
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 ( ) { …. }
}
99 JDBC Umair Javed©2005 100 JDBC Umair Javed©2005
On-line Resources
• Sun’s JDBC Site
– http://java.sun.com/products/jdbc/
• JDBC Tutorial
– http://java.sun.com/docs/books/tutorial/jdbc/