Download Java Database Connectivity (JDBC)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

IMDb wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Oracle Database wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Microsoft Access wikipedia , lookup

Concurrency control wikipedia , lookup

Database wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Database model wikipedia , lookup

Relational model wikipedia , lookup

SQL wikipedia , lookup

Open Database Connectivity wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
Java Database Connectivity (JDBC)
Sun Tutorial
The Sun JDBC tutorial (http://java.sun.com/docs/books/tutorial/jdbc/basics/index.html) covers the
following:
1. Getting Started
2. Setting Up a Database
3. Establishing a Connection
4. Setting Up Tables
5. Retrieving Values from Result Sets
6. Updating Tables
7. Using Prepared Statements
8. Using Joins
9. Using Transactions
10. Stored Procedures
11. SQL Statements for Creating a Stored Procedure
12. Creating Complete JDBC Applications
13. Running the Sample Applications
14. Creating an Applet from an Application
java.sql
The package java.sql contains classes and interfaces for manipulating relational databases in Java.
The java.sql package contains API(Application Programming Interface) for the following:
 Making a connection with a data source
DriverManager class
Driver interface
DriverPropertyInfo class
Connection interface
 Sending SQL statements to a database
Statement interface for sending basic SQL statements
PreparedStatement interface for sending prepared statements or basic SQL statements
(derived from Statement)
CallableStatement interface for calling database stored procedures (derived from
PreparedStatement)
 Retrieving and updating the results of a query
ResultSet interface
 Mapping an SQL value to the standard mapping in the Java programming language
Array interface
Blob interface
Clob interface
Date class
Ref interface
Struct interface
Time class
Timestamp class
Types class
 Custom mapping an SQL user-defined type to a class in the Java programming language
SQLData interface
SQLInput interface
SQLOutput interface
 Providing information about the database and the columns of a ResultSet object
DatabaseMetaData interface
ResultSetMetaData interface
 Throwing exceptions

SQLException thrown by most methods when there is a problem accessing data and by
some methods for other reasons
SQLWarning thrown to indicate a warning
DataTruncation thrown to indicate that data may have been truncated
BatchUpdateException thrown to indicate that not all commands in a batch update
executed successfully
Providing security
SQLPermission interface
JDBC API
Introduction
The JDBC API is the application programming interface that provides universal data access for the Java
programming language. It includes the JDBC 1.0 API, which provides the basic functionality for data
access. The JDBC 2.0 API supplements the basic API with more advanced features and provides a standard
way to access the latest object-relational features being supported by today's relational database
management systems. In addition, the new API includes features such as scrollable and updatable result
sets and improved performance. It also extends JDBC technology beyond the client to the server with
connection pooling and distributed transactions.
Connection
A Connection object represents a connection with a database. A connection session includes the SQL
statements that are executed and the results that are returned over that connection. A single application can
have one or more connections with a single database, or it can have connections with many different
databases.
A user can get information about a Connection object's database by invoking the Connection.getMetaData
method. This method returns a DatabaseMetaData object that contains information about the database's
tables, the SQL grammar it supports, its stored procedures, the capabilities of this connection, and so on.
DriverManager
The DriverManager class is the traditional management layer of JDBC, working between the user and the
drivers. It keeps track of the drivers that are available and handles establishing a connection between a
database and the appropriate driver. In addition, the DriverManager class attends to things like driver login
time limits and the printing of log and tracing messages.
For simple applications, the only method in the DriverManager class that a general programmer needs to
use directly is DriverManager.getConnection. As its name implies, this method establishes a connection to
a database. An application may call the DriverManager methods getDriver, getDrivers, and registerDriver
as well as the Driver method connect, but in most cases it is better to let the DriverManager class manage
the details of establishing a connection.
Statement
A Statement object is used to send SQL statements to a database. There are actually three kinds of
Statement objects, all of which act as containers for executing SQL statements on a given connection:
Statement, PreparedStatement, which inherits from Statement, and CallableStatement, which inherits from
PreparedStatement. They are specialized for sending particular types of SQL statements; a Statement
object is used to execute a simple SQL statement with no parameters, a PreparedStatement object is used to
execute a precompiled SQL statement with or without IN parameters, and a CallableStatement object is
used to execute a call to a database stored procedure.
The Statement interface provides basic methods for executing statements and retrieving results. The
PreparedStatement interface adds methods for dealing with IN parameters; the CallableStatement interface
adds methods for dealing with OUT parameters.
In the JDBC 2.0 core API, the ResultSet interface has a set of new updateXXX methods and other new
related methods that make it possible to update table column values programmatically. This new API also
adds methods to the Statement interface (and PreparedStatement and CallableStatement interfaces) so that
update statements may be executed as a batch rather than singly.
PreparedStatement
The PreparedStatement interface inherits from Statement and differs from it in two ways:
Instances of PreparedStatement contain an SQL statement that has already been compiled. This is what
makes a statement "prepared."
The SQL statement contained in a PreparedStatement object may have one or more IN parameters. An IN
parameter is a parameter whose value is not specified when the SQL statement is created. Instead, the
statement has a question mark ("?") as a placeholder for each IN parameter. The "?" is also known as a
parameter marker. An application must set a value for each question mark in a prepared statement before
executing the prepared statement.
Because PreparedStatement objects are precompiled, their execution can be faster than that of Statement
objects. Consequently, an SQL statement that is executed many times is often created as a
PreparedStatement object to increase efficiency.
Being a subclass of Statement, PreparedStatement inherits all the functionality of Statement. In addition, it
adds a set of methods that are needed for setting the values to be sent to the database in place of the
placeholders for IN parameters. Also, the three methods execute, executeQuery, and executeUpdate are
modified so that they take no argument. The Statement forms of these methods (the forms that take an SQL
statement parameter) should never be used with a PreparedStatement object.
ResultSet
A ResultSet is a Java object that contains the results of executing an SQL query. In other words, it contains
the rows that satisfy the conditions of the query. The data stored in a ResultSet object is retrieved through a
set of get methods that allows access to the various columns of the current row. The ResultSet.next method
is used to move to the next row of the ResultSet, making it the current row.
Mapping SQL and Java Types
Because data types in SQL and data types in the Java programming language are not identical, there needs
to be some mechanism for transferring data between an application using Java types and a database using
SQL types.
In order to transfer data between a database and an application written in the Java programming language,
the JDBC API provides three sets of methods:
Methods on the ResultSet class for retrieving SQL SELECT results as Java types
Methods on the PreparedStatement class for sending Java types as SQL statement parameters
Methods on the CallableStatement class for retrieving SQL OUT parameters as Java types
JDBC Short Course – JDBC Exercises
http://developer.java.sun.com/developer/onlineTraining/Database/JDBCShortCourse/jdbc/exercises/index.h
tml