Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
27 views5 pages

JDBC

JDBC (Java Database Connectivity) is a Java API that enables connection and execution of queries with databases, utilizing different types of JDBC drivers. It includes interfaces such as Statement, PreparedStatement, and Connection, each serving distinct purposes for executing SQL queries and managing database interactions. Additionally, JDBC provides metadata handling capabilities through ResultSet and DatabaseMetaData interfaces to retrieve information about database structures.

Uploaded by

ketandhadve95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

JDBC

JDBC (Java Database Connectivity) is a Java API that enables connection and execution of queries with databases, utilizing different types of JDBC drivers. It includes interfaces such as Statement, PreparedStatement, and Connection, each serving distinct purposes for executing SQL queries and managing database interactions. Additionally, JDBC provides metadata handling capabilities through ResultSet and DatabaseMetaData interfaces to retrieve information about database structures.

Uploaded by

ketandhadve95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

JDBC

What is JDBC?
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query
with the database, and processing the results. It is a part of JavaSE (Java Standard Edition). JDBC API
uses JDBC drivers to connect with the database. There are four types of JDBC drivers:
JDBC-ODBC Bridge Driver
Native Driver
Network Protocol Driver
Thin Driver

What is API?
JDBC also provides support for handling database metadata that allows us to retrieve information
about the database, such as its tables, columns, and indexes.

The JDBC API is comprised of two packages:

 java.sql
 javax.sql

Difference between Statement and Preparedstatement

Feature Statement PreparedStatement

Used when executing a SQL query only Used when executing a SQL query multiple
Purpose
once times

Parameterization Cannot pass parameters at runtime Can pass parameters at runtime

Used for CREATE, ALTER, DROP


Statement Type Used for queries executed multiple times
statements

Performance Typically has lower performance Generally offers better performance

Interface Hierarchy Base interface Extends Statement interface

Query Type Used to execute normal SQL queries Used to execute dynamic SQL queries

Binary Data Reading Cannot use for reading binary data Can use for reading binary data

Usage for DDL


Typically not used for DDL statements Can be used for any SQL Query
Statements

Binary Data Writing Cannot use for writing binary data Can use for writing binary data

Communication No binary protocol used for


Binary protocol is used for communication
Protocol communication


DriverManager class
The DriverManager class is the component of JDBC API and also a member of the java.sql package.
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.

Connection interface
A Connection is a session between a Java application and a database. It helps to establish a
connection with the database.

The Connection interface is a factory of Statement, PreparedStatement, and DatabaseMetaData, i.e.,


an object of Connection can be used to get the object of Statement and DatabaseMetaData. The
Connection interface provide many methods for transaction management like commit(), rollback(),
setAutoCommit(), setTransactionIsolation(), etc.

Commonly used methods of Connection interface:


1) public Statement createStatement(): creates a statement object that can be used to execute SQL
queries.
2) public Statement createStatement(int resultSetType,int resultSetConcurrency): Creates a
Statement object that will generate ResultSet objects with the given type and concurrency.
3) public void setAutoCommit(boolean status): is used to set the commit status. By default, it is true.
4) public void commit(): saves the changes made since the previous commit/rollback is permanent.
5) public void rollback(): Drops all changes made since the previous commit/rollback.
6) public void close(): closes the connection and Releases a JDBC resources immediately.

Statement interface

The Statement interface provides methods to execute queries with the database. The statement
interface is a factory of ResultSet i.e. it provides factory method to get the object of ResultSet.

Commonly used methods of Statement interface:


The important methods of Statement interface are as follows:

1) public ResultSet executeQuery(String sql): is used to execute SELECT query. It returns the object of
ResultSet.
2) public int executeUpdate(String sql): is used to execute specified query, it may be create, drop, insert,
update, delete etc.
3) public boolean execute(String sql): is used to execute queries that may return multiple results.
4) public int[] executeBatch(): is used to execute batch of commands.

import java.sql.*;
class FetchRecord{
public static void main(String args[])throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
Statement stmt=con.createStatement();

//stmt.executeUpdate("insert into emp765 values(33,'Irfan',50000)");


//int result=stmt.executeUpdate("update emp765 set name='Vimal',salary=10000 where id=33");
int result=stmt.executeUpdate("delete from emp765 where id=33");
System.out.println(result+" records affected");
con.close();

}}

PreparedStatement interface
The PreparedStatement interface is a subinterface of Statement. It is used to execute parameterized
query.

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.

What is ResultSetMetaData in JDBC? What is its significance?


The ResultSetMetaData provides information about the obtained ResultSet object like, the number
of columns, names of the columns, datatypes of the columns, name of the table etc…
Following are some methods of ResultSetMetaData class.

Method Description

Retrieves the number of columns in the current


getColumnCount()
ResultSet object.

getColumnLabel() Retrieves the suggested name of the column for use.

getColumnName() Retrieves the name of the column.

getTableName() Retrieves the name of the table.

What is Statement in JDBC?


The Statement interface represents the static SQL statement, it is used to create and execute
general purpose SQL statements using Java programs.

Executing the Statement object


Once you have created the statement object you can execute it using one of
the execute methods namely, execute(), executeUpdate() and,
executeQuery().

 execute(): This method is used to execute SQL DDL statements, it returns a


boolean value specifying weather the ResultSet object can be retrieved.
 executeUpdate(): This method is used to execute statements such as
insert, update, delete. It returns an integer value representing the number of
rows affected.
 executeQuery(): This method is used to execute statements that returns
tabular data (example SELECT statement). It returns an object of the class
ResultSet.

Java DatabaseMetaData interface

DatabaseMetaData interface provides methods to get meta data of a database such as database
product name, database product version, driver name, name of total number of tables, name of
total number of views etc.

Statement :
It is used to execute the DDL queries like create, alter and drop

We can not pass the runtime params to the query

It is used to execute the Query single time

It is slower in performance than PreparedStatement and CallableStatement

PreparedStatment :
It is used to pass the runtime params in the query

It is used when the same query is used multiple times

It has better performance than Statement


It extends Statement interface

It is faster because it is precompiled and query plan is created only one time

even if we are executing the query multiple times

CallableStatement :
CallableStatement is used to call the inbuilt stored procedures

It has better performance than Statement and PreparedStatements because Stored procedures are
already compiled and stored in database memory so, it is faster.

It extends PreparedStatement interface

You might also like