Professional Documents
Culture Documents
Objective
to give some background on JDBC to help with the lab exercises
Overview
1. 2. 3. 4. 5. What is JDBC? The JDBC-ODBC Bridge Four Kinds of JDBC Drivers JDBC Pseudocode
simpJDBC.java
Continued
6. 7. 8.
Meta Data
Books.mdb as an ODBC
9.
More Information
1. What is JDBC?
JDBC is an interface which allows Java code to execute SQL statements inside relational databases
the databases must follow the ANSI SQL-2 standard
JDBC in Use
Java program
JDBC
jdbc-odbc bridge
odbc driver
Lab IV: JDBC
ODBC (Open Database Connectivity) is a Microsoft standard from the mid 1990s. It is an API that allows C/C++ programs to execute SQL inside databases
Continued
The JDBC-ODBC bridge allows Java code to use the C/C++ interface of ODBC
it means that JDBC can access many different database products
The layers of translation (Java --> C --> SQL) can slow down execution.
Continued
1. JDBC-ODBC Bridge
translate Java to the ODBC API
2. Native API
translate Java to the databases own API
Continued
3. Native Protocol
use Java to access the database more directly using its low level protocols
4. Net Protocol use Java to access the database via networking middleware (usually TCP/IP) required for networked applications
Lab IV: JDBC
JDBC Drivers
A searchable list of drivers (freeware, shareware, and commercial) can be found at:
http://developers.sun.com/product/jdbc/driv ers
4. JDBC PseudoCode
Continued
4) Execute a SQL query using a Statement object 5) Get the results in a ResultSet object
DriveManager
creates
Connection
creates
Statement
creates
ResultSet
data
SQL
data
4.2. DriveManager
It is responsible for establishing the connection to the database through the driver. e.g.
e.g. jdbc:odbc:Books
Lab IV: JDBC
Stores the results of a SQL query. A ResultSet object is similar to a table of answers, which can be examined by moving a pointer (cursor).
Continued
Cursor operations:
17 98
Paul Peter
Typical code:
while( rs.next() ) { // process the row; }
Lab IV: JDBC
5. simpJDBC.java
// simpJDBC.java // Displays the firstnames and lastnames // of the Authors table in the Books db. import java.sql.*; public class simpJDBC { public static void main(String[] args) { // The URL for the Books database. // Protected' by a login and password. String url = "jdbc:odbc:Books"; String username = "anonymous"; String password = "guest"; :
Lab IV: JDBC
try { // load the JDBC-ODBC Bridge driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // connect to db using DriverManager Connection conn = DriverManager.getConnection( url, username, password ); // Create a statement object Statement statement = conn.createStatement(); // Execute the SQL query ResultSet rs = statement.executeQuery( "SELECT lastName, firstName FROM Authors" ); :
// Print the result set while( rs.next() ) System.out.println( rs.getString("lastName") + ", " + rs.getString("firstName") ); // Close down statement.close(); conn.close(); } :
catch ( ClassNotFoundException cnfex ) { System.err.println( "Failed to load JDBC/ODBC driver." ); cnfex.printStackTrace(); System.exit( 1 ); // terminate program }
Output
The databases link to the outside (e.g. its ODBC interface) must be configured to have a login and password
details for ODBC are given later
The ResultSet class contains many methods for accessing the value of a column of the current row
can use the column name or position e.g. get the value in the lastName column:
rs.getString("lastName")
Continued
The tricky aspect is that the values are SQL data, and so must be converted to Java types/objects.
There are many methods for accessing/converting the data, e.g.
getString(), getDate(), getInt(), getFloat(), getObject()
Lab IV: JDBC
6. Meta Data
Mark
99 1
meta data
ResultSetMetaData md = rs.getMetaData();
etc.
1. Click on ODBC Data Sources (32bit) in the Control Panel. This displays the ODBC Data Sources Administrator
2. Press Add to add a data source and select Microsoft Access Driver (*.mdb). Press Finish.
3. Type in a source name, description, and press Select to browse to set the path to the Books.mdb file. Now click on Advanced.
Lab IV: JDBC
8. Table in Books.mdb
1 8
Publishers PublisherID PublisherName Titles ISBN Title EditionNumber YearPublished Description PublisherID
1 8 1
8
Lab IV: JDBC
9. More Information
Advanced Java 2 Platform: How to Program Deitel & Deitel, Prentice-Hall, 2001 Chapter 8. http://java.coe.psu.ac.th/ForMember/ Books.html#J2SE Beginning Java 2 Ivor Horton Chapters 18 and 19
Lab IV: JDBC
Continued
Current Information:
http://java.sun.com/products/jdbc/
There are several JDBC books at Aj. Somchai's site, in the Books section
check the publication date