Professional Documents
Culture Documents
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
Topics
1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database Access 5. Modifying the Database Contents 6. Transactions 7. Meta Data 8. Using a GUI to Access a Database 9. Scrollable ResultSets in JDBC 2.0 10. Modifying Databases via Java Methods 11. Using the DataSource Interface.
Introduction to JDBC
JDBC is used for accessing databases from Java applications Information is transferred from relations to objects and vice-versa
- databases optimized for searching/indexing - objects optimized for engineering/flexibility
JDBC Architecture
Oracle
Driver
Oracle
Java Application
JDBC
Network
DB2
Driver
DB2
MySQL Driver
MySQL
4
What is JDBC?
An API that lets you access virtually any tabular data source from the Java programming language
Whats an API? See J2SE documentation Whats a tabular data source? access virtually any data source, from relational databases to spreadsheets and flat files.
JDBC Documentation
General Architecture
What design pattern is implied in this architecture? What does it buy for us? Why is this architecture also multi-tiered?
JDBC
Java is very standardized, but there are many versions of SQL JDBC is a means of accessing SQL databases from Java
JDBC is a standardized API for use by Java programs JDBC is also a specification for how third-party vendors should write database drivers to access specific SQL versions
Driver types
ODBC (Open DataBase Connectivity) is a standard software API designed to be independent of specific programming languages Sun provides a JDBC/ODBC implementation
JDBC Type 2 Driver -- use platform-specific APIs for data access JDBC Type 3 Driver -- 100% Java, use a net protocol to access a remote listener and map calls into vendor-specific calls JDBC Type 4 Driver -- 100% Java
Versions of JDBC
JDBC 1.0 with JDK 1.1 JDBC 2.0 with JDK 1.2 (J2SE 1.2) (commonly used)
JDBC 2.1 with J2SE 1.2.2 J2SE 1.3. server-side database manipulation
JDBC 3.0 with J2SE 1.4. Access from spreadsheets and flat files or any data source Included java.sql and javax.sql
java.sql
The package comprising the core JDBC API is called java.sql We use a simple MS Access database, which means that the inbuilt JDBC-ODBC bridge driver can be employed If you wish to experiment with other databases, place the
steps to set up your own ODBC Data Source(only for ODBC databases!)
class is used to hold methods that operate upon other classes in order to furnish details of their characteristics forName method of class
driver)
<data-source> provides all the information needed by the
5. Manipulate/Display/Check Result(s)
The only method of ResultSet that we need to make use of at
5. Manipulate/Display/Check Result(s)
5. Manipulate/Display/Check Result(s)
Example:
Example:
Example:
Example:
Example:
Example:
Example:
Transactions
Transactions
one or more SQL statements that may be grouped together as a single processing entity We want either all the statements or none of them executed. Java provides the Connection interface methods to be
1. commit
commit/finalise the database changes 2. rollback used to restore the database to the state it was in prior to the current transaction
Transactions
By default, JDBC automatically commits each individual SQL statement that is applied to a database. In order to change this default behaviour so that transaction processing may be carried out, we must first execute Connection
We can then use methods commit and rollback to effect transaction processing.
Transactions
Meta Data
Meta Data
'data about data
Two categories of meta data available through the JDBC API:
data about the rows and columns returned by a query (i.e.,
The first of these is provided by interface ResultSetMetaData, an object of which is returned by the ResultSet method getMetaData
Meta Data
data about the rows and columns is provided by interface ResultSetMetaData, an object of which is returned by the
Data about the database as a whole is provided by interface DatabaseMetaData, an object of which is returned by the Connection method getMetaData
Meta Data
Information available from a object includes the following:
the number of fields/columns in a ResultSet object; the name of a specified field; the data type of a field; the maximum width of a field; the table to which a field belongs.
ResultSetMetaData
Meta Data
SQL types is represented in class java.sql.Types
Meta Data
The example coming up makes use of the following ResultSetMetaData methods, which return properties of the database fields held in a ResultSetMetaData object.
Meta Data
Meta Data
Meta Data
Meta Data
Meta Data
Meta Data
Meta Data
Meta Data
An overloaded form of the Connection method createStatement is used to create scrollable ResultSets. This method that takes two integer arguments.
Scrollable ResultSets in JDBC 2 There are three possible values that <resultSetType> can take:
made
scrollable
updated;
a value of the appropriate type that is to be assigned to the field
1. Updating
There are three steps involved in the process of updating:
2. Insertion
For an insertion, the new row is initially stored within a special buffer called the 'insertion row' and there are three steps involved in the process:
2. Insertion
3. Delete
Example :
Sample Output