JDBC - J D B C: Ava Ata Ase Onnectivity
JDBC - J D B C: Ava Ata Ase Onnectivity
Introduction to JDBC
JDBC is used for accessing databases from Java applications Information is transferred from relations to objects and vice-versa
- databases optimized for searching/indexing - objects optimized for engineering/flexibility
JDBC Architecture
Oracle
Driver
Oracle
Java Application
JDBC
Network
DB2
Driver
DB2
MySQL
Driver
MySQL
3
Seven Steps
Load the driver
An Example
// A driver for imaginary1 Class.forName("ORG.img.imgSQL1.imaginary1Driver");
imaginary1 imaginary2
MySQL
8
Registered Drivers
imaginary1 imaginary2
r
Oracle
Registered Drivers
All are interfaces, hence cannot be instantiated They are created by the Connection
The executeQuery method returns a ResultSet object representing the query result.
18
Timeout
Use setQueryTimeOut(int seconds) of Statement
ResultSet
ResultSet objects provide access to the tables generated
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
ResultSet Methods
Type getType(int columnIndex)
ResultSet Methods
JDBC 2.0 includes scrollable result sets.
23
ResultSet Example
Statement stmt = con.createStatement();
24
Null Values
In SQL, NULL means the field is empty Not the same as 0 or "" In JDBC, you must explicitly ask if the last-read field was null
- ResultSet.wasNull(column)
Null Values
When inserting null values into placeholders of
Prepared Statements:
- Use the method setNull(index, Types.sqlType) for primitive types (e.g. INTEGER, REAL); - You may also use the setType(index, null) for object types (e.g. STRING, DATE).
27
ResultSet Meta-Data
A ResultSetMetaData is an object that can be used to get information about the properties of the columns in a ResultSet object
An example: write the columns of the result set ResultSetMetaData rsmd = rs.getMetaData(); int numcols = rsmd.getColumnCount(); for (int i = 1 ; i <= numcols; i++) { System.out.print(rsmd.getColumnLabel(i)+" "); }
28
Database Time
Times in SQL are notoriously non-standard Java defines three classes to help java.sql.Date
- year, month, day
java.sql.Time
- hours, minutes, seconds
java.sql.Timestamp
- year, month, day, hours, minutes, seconds, nanoseconds - usually use this one
30
Example
Suppose we want to transfer money from bank account 13 to account 72:
PreparedStatement pstmt = con.prepareStatement("update BankAccount set amount = amount + ? where accountId = ?"); pstmt.setInt(1,-100); pstmt.setInt(2, 13); pstmt.executeUpdate(); What happens if this pstmt.setInt(1, 100); update fails? pstmt.setInt(2, 72); pstmt.executeUpdate();
33
Transaction Management
Transactions are not explicitly opened and closed The connection has a state called AutoCommit mode if AutoCommit is true, then every statement is automatically committed
34
AutoCommit
setAutoCommit(boolean val)
If you set AutoCommit to false, you must explicitly commit or rollback the transaction using Connection.commit() and
Connection.rollback()
Note: DDL statements (e.g., creating/deleting tables) in a transaction may be ignored or may cause a commit to occur - The behavior is DBMS dependent
35
Scrollable ResultSet
Statement createStatement( int resultSetType, int resultSetConcurrency) ResultSet.TYPE_FORWARD_ONLY
resultSetType:
ResultSet.TYPE_SCROLL_INSENSITIVE
-backwards, forwards, random cursor movement. -changes made in the database are not seen in the result set object in Java memory. ResultSetTYPE_SCROLL_SENSITIVE
37
-If the given row number is positive, this method moves the cursor to the given row number (with the first row numbered 1).
-If the row number is negative, the cursor moves to a relative position from the last row.
-If the row number is 0, an SQLException will be raised. This method call moves the cursor a relative number of rows, either positive or negative. An attempt to move beyond the last row (or before the first row) in the result set positions the cursor after the last row (or before the first row).
getRow() method retrieves the current row number: The first row is number 1, the second number 2, and so on.
39
- JDO (http://java.sun.com/products/jdo/),
- TopLink (http://www.oracle.com/technology/products/ias/toplink /index.html)
40