* GREY=local Full HTML for

LOCAL foilset Overview of Java Database Connection

Given by Udayan Parvate, Tom Pulikal (Geoffrey C. Fox) at Basic Information Technology Course CPS616 on Spring Semester 1997. Foils prepared 13 March 1997
Abstract * Foil Index for this file

Prepated by Udayan Parvate and Tom Pulikal under Supervision of Wojtek Furmanski
Basic Architecture
Some Current Implementations and Examples

Table of Contents for full HTML of Overview of Java Database Connection


1 JDBC Java - Database Connectivity Overview for CPS616 Technologies of Information Age Spring 97
2 Traditional Web/RDBMS access Compared to using Java
3 JDBC Main Features
4 JDBC Architecture
5 Package java.sql
6 JDBC Classes
7 JDBC API
8 JDBC API - II
9 JDBC API - III
10 JDBC API - IV
11 A typical JDBC API usage sequence
12 Connecting to a database
13 Types of JDBC drivers
14 List of vendors
15 Practical Options at NPAC
16 Practical Options at NPAC contd.
17 Example - Using Imaginary's mSQL Driver
18 Example - using Imaginary's mSQL Driver -II
19 Example - Using Weblogic's Oracle Driver
20 Example - Using Weblogic's Oracle Driver -II
21 Example - Using Weblogic's Oracle Driver -III
22 Example - Using JDBC-ODBC Bridge - I
23 Example - Using JDBC-ODBC Bridge - II
24 Example - Using JDBC-ODBC Bridge -III
25 Example - usingJDBC-ODBC Bridge -IV
26 Example - usingJDBC-ODBC Bridge (contd.)

This table of Contents Abstract



* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 1 JDBC Java - Database Connectivity Overview for CPS616 Technologies of Information Age Spring 97

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
Presented by Geoffrey Fox
Prepared by Udayan Pravate and Tom Pulikal
NPAC Syracuse University 111 College Place, Syracuse NY gcf@npac.syr.edu http://www.npac.syr.edu

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 2 Traditional Web/RDBMS access Compared to using Java

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
Traditional web access to the database was done with the use of CGI script accessing the database and creating output in the form of HTML document presented in WWW client.
The requests and responses were transmitted using HTTP protocol and there was no session notion and it was not able to preserve the database transaction logic
Java programs can be executed on client and on server side.In this case it is possible to create three tier client-server application.
Client applications can also access database directly

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 3 JDBC Main Features

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
"Database-neutral access" interface in java for connecting to database and executing SQL statements
It is designed after ODBC(Open Database connectivity) but with java-specific features
Dependent on database-driver availability
It is also an API for creating the low-level JDBC drivers, which do the actual connecting/transacting with data sources
Based on the X/Open SQL Call Level Interface(CLI) that defines how client/server interactions are implemented for database systems
Programmer writes only one database interface, using jdbc, the pogram can access any data source without recoding

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 4 JDBC Architecture

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
Java applications
Jdbc API
Jdbc Driver manager
Jdbc Driver
Jdbc Driver
DBMS
DBMS
JDBC API
JDBC Driver API
Jdbc-Odbc
Bridge
ODBC Driver
DBMS

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 5 Package java.sql

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
Interface Index
Callable Statement
Connection
DatabaseMetaData
Driver
PreparedStatement
ResultSet
ResultSetMetaData
Statement
Class Index
Date
DriverManager
DriverPropertyInfo
Numeric
Time
Timestamp
Types
Exception Index
DataTruncation
SQLException
SQLWarning

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 6 JDBC Classes

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
DatabaseMetaData This class represents description of database connection which contains implementation details, details about database schema etc
ResultSetMetaData
This class represents the description of the ResultSet class which contains column-names/column types/properties etc.
Statement
This class contains methods to execute SQL statements diretly against the database and to obtain the results
ResultSet
This class provides methods to access data generated by a table query.This includes a series of get methods which retrieve the data in any one of the JDBC Sql type formats, either by column number or by column name

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 7 JDBC API

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
The following are some of the commonly used JDBC API calls
public class Date
This class extends the java.util.Date object. But unlike the java.util.Date, which stores time, this class stores the day,year and month.
StringtoString()
Formats a Date object as YYYY-MM-DD
valueOf(String str)
Converts a String str to an sql.Date object
public class DriverManager
This class is used to load a JDBC Driver . The main methods are
getConnection(String URL,String username ,String password)
throws SQLException
After instantiating a driver using Class.forName(), a connection
object is obtained using getConnection() with a driver URL,
username and password.
getDriver(String URL) throws SQLException
Finds a driver from the registered JDBC drivers

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 8 JDBC API - II

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
BIGINT
BINARY
BIT
CHAR
DATE
DECIMAL
DOUBLE
FLOAT
INTEGER
LONGVARBINARY
LONGVARCHAR
NULL
NUMERIC
REAL
SMALLINT
TIME
TIMESTAMP
VARCHAR etc.
public class Time
This class is a SQL-JDBC data conversion class implementing the
time-storingfunctions
StringtoString()
This returns a String with the time formatted as HH:MM:SS
public class Types
This class contains the SQL data types as constants. It is used by other classes
as the standard constant for the data types. The variables are

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 9 JDBC API - III

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
public interface Callable Statement
This is a primary interface to access stored procedures on a database. The get methods in this interface are identical in functionality returning various datatypes like getInt, getDate etc.
public interface Connection
This is a high level class used to interact with the database. The main methods are
clearWarmings() throws SQLException - clears warnings for the connection
close() throws SQLException - close the connection
commit() throws SQLException - commits the transaction
createStatement() throws SQLException - create statements for queries
getMetaData() throws SQLEXception - returns DatabaseMetaData
isClosed() throws SQLException - Returns true if connection is closed

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 10 JDBC API - IV

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
Public Interface ResultSet
The results of a query are stored in this object, which is returned when the respective query execute methods is run .
getXXX() methods, where XXX can be any Java Types , fetch the result for the specified column
getMetaData() methods can facilitate the process of checking the data type in each column of the result set

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 11 A typical JDBC API usage sequence

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
DriverManager
getConnection()
method returns a
Connection Object
Connection
Statement
ResultSet
Column values
from Query results
method returns a
Statement object
createStatement()
executeQuery()
next()
method returns a
ResultSet Object
method gets the
next row

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 12 Connecting to a database

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
The DriverManager class is used to open a connection to a database via a JDBC Driver, which must be registered with the DriverManager
DriverManager chooses from a given listof available drivers to suit the explicit type of database connection
DriverManager maps URL's to drivers
URL syntax
jdbc:&#060subprotocol&#062:&#060subname&#062
e.g jdbc:msql:naos://naos.npac.syr.edu:2007//stuff
The above syntax specifies the transport to use (jdbc), the database type (msql), the server name (naos), the port (2007), and the database to connect to (stuff).

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 13 Types of JDBC drivers

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
JDBC drivers fit into one of four categories:
1.The JDBC-ODBC bridge provides JDBC access via mostly ODBC drivers.
2.A native-API partly-Java driver converts JDBC calls into calls to the client API for Oracle, Sybase, Informix, DB2, or other DBMS.
( E.g. WebLogic Oracle driver)
3.A net-protocol all-Java driver translates JDBC calls into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server. This net server middleware is able to connect all its Java clients to many different databases.
4.A native-protocol all-Java driver converts JDBC calls into the network protcol used by DBMSs directly. This allows a direct call from the client machine to the DBMS server and is a practical solution for Intranet access. (also WebLogic)

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 14 List of vendors

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
Some of the leading database, connectivity and tools vendors who have already endorsed the JDBC API and the DBMS's they support are
Borland International Inc. - InterBase 4.0
IBM - DB2 Version 2
Imaginary Software Inc. - mSQL
Intersolv - Oracle and Sybase
Javasoft - Several dozen through ODBC drivers
Sybase Inc. - Sybase SQL server
Weblogic Inc., etc. - Oracle, Sybase, MS SQL server

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 15 Practical Options at NPAC

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
The JDBC technologies that are currently being explored at NPAC are :
mSQL Database - JDBC driver from Imaginary
- a lightweight SQL engine that supports a subset of ANSI SQL
- shareware product that can be built on most Unix OS and Win32 OS
- one of the first database products to be supported by a Java API
- avialable from anonymous ftp ftp://bond.edu.au/pub/Minerva/msql
- JDBC driver for mSQL database can be downloaded from
http://www.imaginary.com/Java/
Oracle - JDBC driver from Weblogic
- Weblogic has got a set of proprietary solutions to access DB
- Works with Oracle, Sybase, MS SQL server
- Classes built on top of JDBC to handle result datasets etc.
- Access the DB either through JDBC application or applets
- Test versions of their products can be downloaded from
http://www.weblogic.com

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 16 Practical Options at NPAC contd.

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
Odbc using Jdbc-Odbc bridge from Intersolv Inc.
- ODBC is an open programming interface based on structured query language (SQL). The interface provides a standard, universal library of functions for building and deploying cross-platform database-independent applications.
-ODBC functions can be called from any programming language that can call a dynamic link library (DLL) or shared library.
-"ODBC is not appropriate for direct use from Java since it is a C interface."
-Non-availability of database drivers prompted javasoft to leverage the broad base of installed ODBC drivers, so the JDBC/ODBC Bridge enables data access from Java to ODBC interface via JDBC

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 17 Example - Using Imaginary's mSQL Driver

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
import java.net.URL;
import java.sql.*;
import imaginary.sql.*;
class Select {
public static void main (String argv[] ) {
try {
//load the class imaginary/sql/iMsqlDriver
Class.forName (" imaginary.sql.iMsqlDriver");
String url = jdbc:msql://host.npac.syr.edu:4333/test";
//connect to the test database using the driver URL
Connection conn = DriverManager.getConnection( url, "scott", "tiger");
//create the query statement and get the result set
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery (" select * from
test_table order by id");
System.out.println ("Got results :");
//print the results in a loop
while (rs.next() ) {
int a = rs.getInt(1);
String str = rs.getString(2);
System.out.println("key = " + a);
System.out.println("str = " + str); }

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 18 Example - using Imaginary's mSQL Driver -II

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
//close the query execution and the database
connection before exiting
stmt.close();
conn.close();
}
//catch any exception and print it to the
system output
catch ( Exception e) {
System.out.println (e.getMessage() );
e.printStackTrace();
}
} // End main
} // End class Select

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 19 Example - Using Weblogic's Oracle Driver

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
package tutorial.jdbc.oracle;
import java.sql.*;
public class myTest {
public static void main (String argv[] ) throws Exception
{ //load the driver class from the local CLASSPATH
Class.forname("weblogic.jdbc.oci.Driver");
//connect to the database using the driver url and
// other properties like username, password and server
Connection conn =
DriverManager.getConnection("jdbc:weblogic:oracle:kayak",
"scott", "tiger")
//statement class is instantiated using the connection
// class for executing queries
Statement stmt = conn.createStatement();
stmt.execute("select * from empdemo");

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 20 Example - Using Weblogic's Oracle Driver -II

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
//result set or hit list is obtained after
// executing the query
//and using a cursor it is displayed
ResultSet rs = stmt.getResultSet ( );
while (rs.next () ) {
System.out.println( rs.getString(1) + "-" +
rs.getString(2) + "-" + rs.getString(3) );
}
//get the metadata using the result set
ResultSetMetaData rsmd = rs.getMetaData ();
System.out.println("Number of Columns:"
+ rsmd.getColumnCount() );

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 21 Example - Using Weblogic's Oracle Driver -III

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
//display the metadata information like max size. column type name etc.
for (int i = 1; i &#060= rsmd.getColumnCount(); i++) {
System.out.println("Column Name :" + rsmd.getColumnName(i));
System.out.println("Nullable :" + rsmd.isNullable(i));
System.out.println("Precision :" + rsmd.getPrecision(i));
System.out.println("Scale :" + rsmd.getScale(i));
System.out.println("Size :" + rsmd.getColumnDisplaySize(i));
System.out.println("Column Type :" + rsmd.getColumnType(i));
System.out.println("ColType Name :" + rsmd.getColumnTypeName(i));
System.out.println(" ");
}
//close the query connection states and then close the database connection
stmt.close();
conn.close();
} // end main
} // end myTest class

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 22 Example - Using JDBC-ODBC Bridge - I

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
import java.net.URL;
import java.sql.*;
class simpleselect {
public static void main (String args[]) {
String url = "jdbc:odbc:my-dsn";
String query = "SELECT * FROM emp";
try {
// Load the jdbc-odbc bridge driver
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
// Attempt to connect to a driver. Each one
// of the registered drivers will be loaded until
// one is found that can process this URL
// SEE NEXT FOIL!!

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 23 Example - Using JDBC-ODBC Bridge - II

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
Connection con = DriverManager.getConnection (
url, "my-user", "my-passwd");
// If we were unable to connect, an exception
// would have been thrown. So, if we get here,
// we are successfully connected to the URL
// Get the DatabaseMetaData object and display
// some information about the connection
DatabaseMetaData dma = con.getMetaData ();
System.out.println("\nConnected to " + dma.getURL());
System.out.println("Driver " +
dma.getDriverName());
System.out.println("Version " +
dma.getDriverVersion());
System.out.println("");

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 24 Example - Using JDBC-ODBC Bridge -III

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
// Create a Statement object so we can submit
// SQL statements to the driver
Statement stmt = con.createStatement ();
// Submit a query, creating a ResultSet object
ResultSet rs = stmt.executeQuery (query);
// Display all columns and rows from the result set
dispResultSet (rs); // see later
// Close the result set
rs.close();
// Close the statement and then the connection
stmt.close();
con.close();
}

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 25 Example - usingJDBC-ODBC Bridge -IV

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
catch (SQLException ex) {
// A SQLException was generated. Catch it and
// display the error information. Note that there
// could be multiple error objects chained
// together
System.out.println ("\n*** SQLException caught***\n");
while (ex != null) {
System.out.println ("SQLState: " +
ex.getSQLState ());
System.out.println ("Message: " +
ex.getMessage ());
System.out.println ("Vendor: " +
ex.getErrorCode ());
ex = ex.getNextException ();
System.out.println ("");
}
}

* GREY=local HTML version of LOCAL Foils prepared 13 March 1997

Foil 26 Example - usingJDBC-ODBC Bridge (contd.)

From Overview of Java Database Connection Basic Information Technology Course CPS616 -- Spring Semester 1997. *
Full HTML Index
catch (java.lang.Exception ex) {
// Got some other type of exception. Dump it.
ex.printStackTrace ();
}
}
} // End Main
// dispResultSet
// Displays all columns and rows in the given result set
private static void dispResultSet (ResultSet rs)
throws SQLException
{
//Display routine for displaying the result set goes here
}
} // End simpleselect

Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Tue Jul 22 1997