Lecture 1 Advanced Java Programming
Lecture 1 Advanced Java Programming
Database Connectivity
JDBC
The JDBC API Components
JDBC
The Application Layer encompasses 3 interfaces that are implemented at the Driver Layer but used at the application layer. Recall: An interface allows a general name to indicate a specific object. The general name defines methods that must be implemented by the specific object classes. The 3 interfaces used at the Application Layer are Connection, Statement and Resultset. A connection object is obtained from the driver implementation through DriverManager.getConnection() method call Once a connection is resturned, a statement object to issue against the database can be created.
Oracle
oracle.jdbc.driver.OracleDriver
DB2
COM.ibm.db2.jdbc.net.DB2Driver
Access
sun.jdbc.odbc.JdbcOdbcDriver
Resultset
The Resultset interface defines methods for accessing tables of data generated by executing a Statement. The table rows are retrieved in sequence. A ResultSet maintains a cursor pointing to its current row of data. The Resultset column values may be accessed in any order; they are indexed and may be selected either by name or the number of the column (from 1 to n). Resultset resultSet = stmt.executeQuery("SELECT * FROM Authors");
ResultsetMetaData
ResultSetMetaData Interface holds information on the types and properties of the columns in a ResultSet. It is constructed from the Connection object.
PreparedStatement
If an SQL statement needs to be executed many times but with different values, a prepared statement can be used to improve performance. For example, if on a website users look up product information with a product id using the same query each time, a prepared statement should be used. A prepared statement is a precompiled SQL statement and its use saves the database from repeatedly having to compile the SQL statement each time it is executed. A query in a prepared statement contains placeholders (represented by the '?' character) instead of explicit values. The values for these placeholders can be set and then the prepared statement is executed.