Java Reference
In-Depth Information
accessing relational dataBases from Java
Accessing relational databases from Java is not a straightforward exercise. Various issues arise due
to the intrinsic conceptual differences between Java as an object-oriented programming environ-
ment and the database, which is typically based on relational concepts and SQL. The discrepancy
between both environments is often referred to as the impedance mismatch problem .
The first problem concerns the incompatibility between the data types implemented in Java and the
types available in SQL. Some SQL data types are directly equivalent to a Java type. An example
of this is the SQL INTEGER data type, which is identical to the Java int data type. Other SQL data
types need to be converted. Examples are the SQL CHAR , VARCHAR , and LONGVARCHAR data types,
which can be easily converted to the Java equivalent String data type. For the SQL DATE data type,
a special Java data type, Java.Date , was created.
Another complication is that SQL relations are sets of records with no prior limitation on the
number. Hence, Java needs to foresee a mechanism to appropriately import and handle the results
of SQL queries. One popular approach here is a cursor mechanism that will allow you to iteratively
loop through a set of records and process them one at a time in Java.
A discussion on the various ways of accessing databases from Java, each with their own approach
to dealing with both of these problems, follows. It begins with an introduction to JDBC, which is a
standard database application programming interface (API) available in Java. This is followed by a
discussion of SQLJ, which allows SQL statements to be directly embedded into Java programs.
Java database connectivity (JdBc)
Every database vendor provides its own application programming interface (API). Hence, when
connecting to a particular database, you need to be aware of the proprietary routines that make up
the particular API. This makes it tedious and difficult to use different databases in an application.
Java database connectivity (JDBC) is a popular standardized application programming interface
(API) that gives database-independent access to tabular data typically stored in relational databases
or Microsoft Excel. It allows you to set up a connection with the database (or tabular data source),
exchange SQL statements (such as CREATE , SELECT , INSERT , UPDATE , and DELETE ), and process the
results. A key advantage of JDBC is that it provides an easy access mechanism to various existing
data sources. Moreover, since it is based on Java technology, it is easy to learn and use in your Java
programs. Given its popularity, many database vendors provide support for it, such as Oracle, IBM,
Microsoft, and more. The most recent version is JDBC 4.2 and is included in Java SE 8. The JDBC
classes are implemented using the packages java.sql and javax.sql . There are four types of JDBC
drivers to access the data, as shown in Table 9-4.
taBleĀ 9-4: JDBC Driver Types
driver t ype
description
JDBC type 1
driver
JDBC/ODBC bridge. Open Database Connectivity (ODBC) is another standard API
for accessing databases developed by Microsoft. This driver will convert JDBC calls
to client-side ODBC calls, which then communicate with the network database.
continues
 
Search WWH ::




Custom Search