Java SQL Package

In Java programming language, the java.sql package contains many classes and interfaces for database connectivity. In this post, we will discuss the important classes and interfaces of the Java SQL package.

The latest JDBC API is mainly divided into two packages,

1. java.sql package:- This package includes the classes and interfaces to perform almost all JDBC operations such as creating and executing SQL queries. It provides the API for accessing and processing data that is stored in the database. It includes a framework where different drivers can be installed dynamically to access different databases (especially relational databases).

2. javax.sql package:- This package is also known as JDBC extension API. It provides classes and interfaces to access server-side data.

Classes and interface of Java SQL package to make a connection with the database

These classes and interfaces are used for making a connection to the database with the help of the DriverManager class. The DriverManager class is the base class for all JDBC program.

  • DriverManager class:- It helps to make a connection with the driver.
  • Driver Interface:- It is mainly used by the DriverManager class for registering and connecting drivers based on JDBC technology.
  • SQLPermission class:- It provides permission when the code is running within a security manager such as applet. It attempts to set up a logging stream within the DriverManager class.
  • DriverPropertyInfo class:- It is only of interest to advanced programmers who need to interact with a Driver via getDriverProperties to discover and supply properties for connections. It is generally not used by the general user.

Interface of Java sql package to send SQL parameters to a database

  • Statement interface:- It is used to send basic SQL statements.
  • PreparedStatement interface:- It is used to send a prepared statement or derived SQL statements from the Statement object.
  • CallableStatement interface:- It is used to call database stored procedures/functions.
  • Connection interface:- It provides methods for creating statements and managing their connections and properties.
  • Savepoint:- It helps to make the savepoints in a transaction.

Interface for updating and retrieving results of a query

ResultSet interface:- It maintains a cursor pointing to its current row of data. The cursor is initially positioned before the first row. The next() method of the resultset interface moves the cursor to the next row and it will return false if there are no more rows in the ResultSet object. By default ResultSet object is not updatable and has a cursor that moves forward only.

Interfaces of Java SQL package for Metadata

Data about data is called MetaData. Gathering more info about given info or providing more info about the given info is called MetaData. In Java, it is code about code, or info about info, either gathering info or providing info. In JDBC metadata concepts are given to gather more info about database software, tables, and query params.

  • DatabaseMetaData interface:- It provides information about the database. It is used to gather more info about underlying Database software and its limitations and capabilities.
  • ResultSetMetaData interface:- It gives the information about the columns of a ResultSet object. It is used to gather more info about Database tables represented by ResultSet objects like column names, column data types, column size e.t.c.
  • ParameterMetaData interface:- It gives more info about parameters (?) that are there in SQL Queries represented by PreparedStatement object, CallableStatement object.

Classes of Java SQL package for Exceptions

  • SQLException class:- It is thrown by the methods whenever there is a problem while accessing the data or any other things.
  • SQLWarning class:- This exception is thrown to indicate the warning. It is a subclass of SQLException. It provides information on database access warnings. Warnings are silently chained to the object whose method caused it to be reported. Warnings may be retrieved from Connection, Statement, and ResultSet objects. Trying to retrieve Connection/Statement after it has been closed will cause an exception to be thrown.
  • BatchUpdateException class:- The exception is thrown to indicate that all commands in a batch update are not executed successfully. It is a subclass of SQLException. It also provides the update counts for all commands that were executed successfully during the batch update, that is, all commands that were executed before the error occurred.
  • DataTruncation class:- It is thrown to indicate that the data may have been truncated. It is a subclass of SQLWarning.
  • Other Exceptions of Java SQL package are SQLClientInfoException, SQLDataException, SQLFeatureNotSupportedException, SQLIntegrityConstraintViolationException, SQLInvalidAuthorizationSpecException, SQLNonTransientConnectionException, SQLNonTransientException, SQLRecoverableException, SQLSyntaxErrorException, SQLTimeoutException, SQLTransactionRollbackException, SQLTransientConnectionException, SQLTransientException

Mapping for SQL types/user

The classes and interfaces of Java SQL package for providing Standard mapping for SQL types.

Interface/classMapping for
Array interfaceSQL Array
Blob interfaceSQL Blob
Clob interfaceSQL Clob
Date classSQL Date
Ref interfaceSQL Ref
Struct interfaceSQL Struct
Time classSQL Time
Timestamp classSQL Timestamp
Types classSQL types

The classes and interfaces of Java SQL package for providing a custom mapping to an end user-defined type (UDT) to a class.

  • SQLData interface:- It gives the mapping of a UDT to an instance of this class.
  • SQLInput interface:- It gives the methods for reading UDT attributes from a stream.
  • SQLOutput:- It gives the methods for writing UDT attributes back to a stream.

Important classes and interface of javax.sql package

  • javax.sql.ConnectionEvent:- It provides information about the occurrence of the event.
  • javax.sql.ConnectionEventListener:- It is used to register event generated by PooledConnection object.
  • javax.sql.DataSource:- It represents the DataSource interface used in an application.
  • javax.sql.PooledConnection:- It provide object to manage connection pools.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Also Learn,

1 thought on “Java SQL Package”

Leave a Comment

Your email address will not be published. Required fields are marked *