JDBC CS 122
JDBC zJava Database Connectivity zDatabase Access Interface Õprovides access to a relational database (by allowing SQL statements to be sent and executed through a Java program) ÕJDBC package: set of Java classes that facilitate this access (java.sql.*) zComes with JDK (since 1.1)
JDBC Driver Need a driver, specific to the DB product, to mediate between JDBC and the database Õthe driver is a Java class that needs to be loaded first Relational DBMS Java Program - load driver - establish connection - send SQL statements
JDBC-ODBC Bridge zDriver that interfaces with ODBC (Object Database Connectivity--also an access interface) zEasiest way to access databases created by Microsoft products Õregister database as an ODBC data source Õuse JDBC-ODBC bridge as the JDBC driver (included in JDK 1.2 distribution)
Key Classes in JDBC zConnection Õneed to create an instance of this class when establishing a connection to the database zStatement Õfor issuing SQL statements zResultSet (interface) Õa ResultSet object represents the table returned by an SQL select statement
Establishing a Connection Use the getConnection() method Õunder the DriverManager class ÕString argument: "jdbc:driver:name” Õreturns a Connection object Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); // above line loads the jdbc-odbc driver String dbname = “jdbc:odbc:MyDB”; Connection c = Driver.getConnection(dbname);
Creating a Statement Object Execute the createStatement() method on the Connection object Õreturns a Statement object Õafterwards, run methods on the Statement object to execute an SQL statement Statement s = c.createStatement();
Methods of the Statement Class zexecuteQuery() Õrequires a String argument (a select statement) Õreturns a ResultSet object zexecuteUpdate() Õrequires a String argument (an insert, update, or delete statement) Õreturns an int (row count, in most cases)
The ResultSet Interface zA ResultSet object represents the table returned by the select statement sent zNavigation/retrieval methods Õnext(): moves to the next row (first row if called for the first time), returns false if no rows remain ÕgetXXX methods return the value of a field for the current row
get Method Example: getInt() ResultSet rs; rs = s.executeQuery(“SELECT * FROM Orders”); rs.next(); // gets the first row // suppose the Orders table has an integer field // called quantity int myvar = rs.getInt(“quantity”); // if you knew that quantity is the 2nd field in the table myvar = rs.getInt(2);
Exercise zCreate a Microsoft Access table Õinsert sample rows zAdd an ODBC data source Õuse the Microsoft Access driver Õassociate with the created database zCreate a Java program Õuse JDBC-ODBC bridge Õcreate a loop that lists all rows of the table
Summary zJDBC allows you to write Java programs that manipulate a database zA driver (often a separate product) is required that facilitates access zKey classes: Connection, Statement, and ResultSet zOther features: metadata, parameterized statements, and stored-proc invocation