Summary
JDBC 1.2 from JDK 1.1.x, JDBC 2.0 for JDK1.2/Java 2 to do batch updates and forward, backward, jumping around records with a result set. JDBC 3.0 from JDK1.4 added SQL3 BLOB/CLOB datatypes, batch updates, JDBC extension API for JavaBeans, the JDBC Optional Package for JNDI and connection pooling. JDBC 1.5 for Java 1.5 The JBoss commercial open-source database-access program Hibernate 3 for the JBoss Application Server 4 (July 2005) are designed to comply with the EJB 3.0 spec, a Java standard meant to make it easier to build server-side Java programs. |
Watch for Caribou Lake offers products to 1) compare an ordered set of rows between two JDBC tables 2) view, search, and update a database from a web browser, 3) transfer of data from one JDBC table to another like table in a different JDBC database. 4) compute a checksum on any set of rows in a table from a JDBC data source 5) pools, reuses, and serves JDBC connections to multi-threaded Java applications |
Database Drivers
Because databases connections are abstract, a single interface can accomodate the different protocols used by various databases. This allows developers to access data across incompatible database management systems running on varied platforms. As of 5/15/03, over 178 java.sql database drivers are listed or certified by Javasoft
|
Java Database Connectivity Computerworld article by Carol Sliwa Dec 13, 1999 Ken North's list of JDBC Drivers, Servers, and Vendors with an explanation Microsoft Jet 4.0 Service Pack 4 (SP4) updates the Microsoft Jet 4.0 database engine. Microsoft Knowledge Base: Q282010 ACC2000: Updated Version of Microsoft Jet 4.0 Available in Download Center First Published: May 7 2001 12:24PM
|
Create a Data Source
odbcad32
Some programs place this in the Control Panel as the "ODBC Data Sources (32bit)" icon. or Administrator Tools
|
1. Register Database Driver
|
3. Create a Statement object
Statement stmt = con.createStatement();
To obtain an updatable ResultSet, specify scroll-sensitive and updatable parameters.
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
To update the 9th row in a scrollable ResultSet
resultSet.absolute(9);
resultSet.updateString("FIELDNAME", "VALUE"); resultSet.updateRow(); JDBC allows programmers to update multiple data items with a single command, or even access multiple database servers within a single transaction. In addition, it lets programmers reuse database connections with connection pooling) so a new connection doesn't need to be made to a database for each new JDBC command. The PrepareStatement object is the method of sending SQL to the database. A PreparedStatement object holds reusable SQL statements.
MetaDataGet column names from a query through the ResultSetMetaData associated with the object:
ResultSetMetaData rsmd = rs.getMetaData();
for( int i=0; i<rsmd.getColumnCount(); i++ ) {
|
4. Prepare Database with executeUpdate Statements
|
5. Catch and Handle ErrorsJavaEach SQLException object provides several kinds of information:A string describing the error. This is used as the Java Exception message, available via the method getMessage(). The DatabaseMetaData getSQLStateType method of the SQL 99 or XOPEN SQL specification can discover whether a "SQLstate" string was returned. An integer error code that is specific to each vendor. Normally this will be the actual error code returned by the underlying database. This information is available via the getErrorCode() method. A chain to a next Exception. This can be used to provide additional error information by invoking the getNextException() method.
catch(SQLException ex) {
System.out.println("SQLState: " + ex.getSQLState ()); System.out.println("ErrorCode: " + ex.getErrorCode ()); ex = ex.getNextException(); System.out.println(""); Additional code is needed to catch warnings. MS-Access "command text was not set for the command object"Microsoft Access uses Jet, and Jet is thread safe starting from Jet 4.0. From MDAC 2.1 and later, you can have connection pooling for Access ODBC driver. Connections that use older Jet OLE DB providers and ODBC drivers are not pooled because those providers and drivers do not support pooling. The connection continues to remain open and unused until the connection times out. The default timeout with connection pooling enabled for SQL Server is 60 seconds.An ODBC application can enable connection pooling by calling SQLSetEnvAttr with the SQL_ATTR_CONNECTION_POOLING attribute. 164221 Implicitly setting the ADO ActiveConnection (Conn) to a connection string defeats efficiencies from connection pooling, one the new features introduced in ODBC driver manager 3.0. Connection pooling enables an ODBC application to reuse a connection from a pool of connections. Once a connection has been created and placed in the pool, an ODBC application can reuse the same driver and the connection within the same shared environment (henv) without performing the complete connection process. Set it to a connection object. 191572 INFO: Connection Pool Management by ADO Objects Called From ASP |
6. Using ResultSet Returned from executeQuery
The next() method moves the cursor to the next row (similar to an iterator or an enumerator in Java).
getString()
The absolute() method moves the cursor to a specific row in the result set. The JDBC driver attempts to convert the underlying data to the specified Java type and returns a suitable Java value. The next fragment uses several methods of the ResultSet class in a while loop to display all of the data in the ResultSet object produced by the earlier query.
System.out.println("Display all results:");
while( rs.next() ) {
String str = rs.getString("test_val"); if !wasNull( "test_val" ){ \\ not SQL null
System.out.println("\ttest_id= " + theInt + "\tstr = " + str); Methods getString(), getInt() retrieve column values for the current row. You can retrieve values using either the index number of the column or the name of the column. In general, using the column index is more efficient. The boolean wasNull() method is needed so that nulls are ignored. To avoid incompatibilities, test with it instead of the Java base class Null literal.
|
SQL3 data typesBLOBs and Other SQL3 Advanced DatatypesJDBC defaults all Connection objects to auto-committing database transactions immediately after they are issued. But support of component transactions requires this to be turned off so that a JDBC-specific implementation of the Transaction class can manage the commit logic.
con.setAutoCommit( false );
|
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page
Thank you! |