JDBC Classes
1- Introduction to JDBC Classes
• JDBC provides several important classes for database connectivity
and data manipulation. Here are some key JDBC classes and their
explanations:
DriverManager Class
• The DriverManager class is responsible for managing database
drivers. It provides methods for registering and obtaining JDBC
drivers based on database URLs.
• The DriverManager class is the component of JDBC API and also a
member of the java.sql package.
DriverManager Class
• The DriverManager class acts as an interface between users and
drivers. It keeps track of the drivers that are available and handles
establishing a connection between a database and the appropriate
driver.
• It contains all the appropriate methods to register and deregister the
database driver class and to create a connection between a Java
application and the database.
Methods Of DriverManager Class
1. public static synchronized void registerDriver(Driver driver): is used
to register the given driver with DriverManager. No action is
performed by the method when the given driver is already
registered.
2. public static synchronized void deregisterDriver(Driver driver): is
used to deregister the given driver (drop the driver from the list)
with DriverManager. If the given driver has been removed from the
list, then no action is performed by the method.
Methods Of DriverManager Class
3. public static Connection getConnection(String url) throws
SQLException: is used to establish the connection with the specified
url. The SQLException is thrown when the corresponding Driver
class of the given database is not registered with the
DriverManager.
4. public static Connection getConnection(String url,String
userName,String password) throws SQLException: is used to
establish the connection with the specified url, username, and
password. The SQLException is thrown when the corresponding
Driver class of the given database is not registered with the
DriverManager.
Connection with DriverManager
• Connection connection =
DriverManager.getConnection(url, user,
password);
Create Statement Object
• The createStatement() method of Connection interface is used to
create statement.
• The object of statement is responsible to execute queries with the
database.
Create Statement Object
• Syntax of Statement creation object:
Statement statement =
connection.createStatement();
Statement Object executeQuery method
• The executeQuery() method of Statement interface is used to
execute queries to the database.
• This method returns the object of ResultSet that can be used to get
all the records of a table.
Statement Object executeQuery method
Syntax
• ResultSet resultSet =
statement.executeQuery(query);
PreparedStatement
• The PreparedStatement interface is a subinterface of Statement. It is
used to execute parameterized query.
• Let's see the example of parameterized query:
• String query = "insert into users (name,
profile_image) values(?, ?)";
PreparedStatement
• As you can see, we are passing parameter (?) for the values.
• Its value will be set by calling the setter methods of
PreparedStatement.
Why use PreparedStatement?
• Improves performance: The performance of the application will be
faster if you use PreparedStatement interface because query is
compiled only once.
How to create PreparedStatement object
• PreparedStatement statement =
connection.prepareStatement(query);
Methods of PreparedStatement interface
• The important methods of PreparedStatement interface are given
below: