Wednesday, October 1, 2014

2.10 JDBC APIs for Database Connectivity (java.sql package)


2.10 JDBC APIs for Database Connectivity (java.sql package)

-          If any java application or an applet wants to connect with a database then there are various classes and interfaces available in java.sql package.
-          Depending on the requirements these classes and interfaces can be used.
-          Some of them are list out the below which are used to perform the various tasks with database as well as for connection.
-          Provides the API for accessing and processing data stored in a data source (usually a relational database) using the Java programming language.
-          This API includes a framework whereby different drivers can be installed dynamically to access different data sources.
-          Although the JDBC API is mainly geared to passing SQL statements to a database, it provides for reading and writing data from any data source with a tabular format.
-          Although the JDBC API is mainly geared to passing SQL statements to a database, it provides for reading and writing data from any data source with a tabular format.

-          The reader/writer facility, available through the javax.sql.RowSet group of interfaces, can be customized to use and update data from:
1)       Spread sheet,
2)      Flat file, or
3)      Tabular data source.

-          java.sql package, referred to as the JDBC core API.
-          And the javax.sql package, referred to as the JDBC Optional Package API.
-          This complete JDBC API is included in the Java Standard Edition (Java SE), version 6.
-          The javax.sql package extends the functionality of the JDBC API from a client-side API to a server-side API.
-          And it is an essential part of the Java Enterprise Edition (Java EE) technology.
-          The class SQLPermission was added in the Java 2 SDK, Standard Edition, version 1.3 release.
-          This class is used to prevent unauthorized access to the logging stream associated with the DriverManager.

-          Which may contain information such as:
1)      Table names,
2)      Column data, and
3)      So on…

RDBMS
JDBC driver name
URL format
Access
sun.jdbc.odbc.JdbcOdbcDriver
jdbc:odbc:<database_name>
MySQL
com.mysql.jdbc.Driver
jdbc:mysql://hostname/ databaseName
ORACLE
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@hostname:port Number:databaseName

-          Some of them are list out the below which are used to perform the various tasks with database as well as for connection.
-           
Class or Interface
Description
Java.sql.Connection
Create a connection with specific database
Java.sql.DriverManager
The task of DriverManager is to manage the database driver
Java.sql.Statement
It executes SQL statements for particular connection and retrieve the results
Java.sql.PreparedStatement
It allows the programmer to create prepared SQL statements
Java.sql.CallableStatement
It executes stored procedures
Java.sql.ResultSet
This interface provides methods to get result row by row generated by SELECT statements
-           
v What the java.sql Package Contains :

-          The java.sql package contains API for the following:

  • Making a connection with a database via the DriverManager facility :

1)       DriverManager class :
§  Makes a connection with a driver

2)       SQLPermission class :
§  Provides permission when code running within a Security Manager, such as an applet, attempts to set up a logging stream through the DriverManager.

3)       Driver interface :
§  Provides the API for registering and connecting drivers based on JDBC technology ("JDBC drivers");
§  generally used only by the DriverManager class

4)       DriverPropertyInfo class :
§  Provides properties for a JDBC driver;
§  Not used by the general user

  • Sending SQL statements to a database :

1)       Statement :
§  Used to send basic SQL statements.
§  The Statement interface is used for to execute a static query.

2)       PreparedStatement :
§  Used to send prepared statements or basic SQL statements (derived from Statement)

3)       CallableStatement :
§  Used to call database stored procedures (derived from PreparedStatement)

4)       Connection interface :
§  Urovides methods for creating statements and managing connections and their properties

5)       Savepoint :
§  Provides savepoints in a transaction

  • Retrieving and updating the results of a query :

1)       ResultSet interface :

  • Standard mappings : (for SQL types to classes and interfaces in the Java programming language)

1)       Array interface :
§  Mapping for SQL ARRAY

2)       Blob interface :
§  Mapping for SQL BLOB

3)       Clob interface
§  Mapping for SQL CLOB

4)       Date class :
§  Mapping for SQL DATE

5)       NClob interface :
§  Mapping for SQL NCLOB

6)       Ref interface :
§  Mapping for SQL REF

7)       RowId interface :
§  Mapping for SQL ROWID

8)       Struct interface :
§  Mapping for SQL STRUCT

9)       SQLXML interface :
§  Mapping for SQL XML

10)   Time class :
§  Mapping for SQL TIME

11)   Timestamp class :
§  Mapping for SQL TIMESTAMP

12)   Types class :
§  provides constants for SQL types

  • Custom mapping : (an SQL user-defined type (UDT) to a class in the Java programming language)

1)       SQLData interface :
§  Specifies the mapping of a UDT to an instance of this class

2)       SQLInput interface :
§  Provides methods for reading UDT attributes from a stream

3)       SQLOutput interface :
§  Provides methods for writing UDT attributes back to a stream

  • Metadata :
1)       DatabaseMetaData interface :
§  Provides information about the database

2)       ResultSetMetaData interface :
§  Provides information about the columns of a ResultSet object

3)       ParameterMetaData interface :
§  Provides information about the parameters to PreparedStatement commands

  • Exceptions :

1)       SQLException :
§  Thrown by most methods when there is a problem accessing data and by some methods for other reasons

2)       SQLWarning :
§  Thrown to indicate a warning

3)       DataTruncation :
§  Thrown to indicate that data may have been truncated

4)       BatchUpdateException :
§  Thrown to indicate that not all commands in a batch update executed successfully

No comments:

Post a Comment