Unit 5-Database Connectivity
Unit 5-Database Connectivity
• 12 MARKS
JDBC
• JDBC stands for Java Database Connectivity.
JDBC is a Java API to connect and execute the
query with the database.
• It is a part of JavaSE (Java Standard Edition).
JDBC API uses JDBC drivers to connect with
the database.
• We can use JDBC API to access tabular data stored in
any relational database.
• By the help of JDBC API, we can save, update, delete
and fetch data from the database. It is like Open
Database Connectivity (ODBC) provided by Microsoft.
• The current version of JDBC is 4.3. It is the stable
release since 21st September, 2017
• The java.sql package contains classes and interfaces
for JDBC API.
Why Should We Use JDBC
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
5) Close the connection object
• Create Statement
• Prepared Statement
• Callable Statement
1. Create a Statement:
• catch (ClassNotFoundException e)
• { // Print and display the line number // where exception occurred
2. Prepared Statement:
• A PreparedStatement represents a
precompiled SQL statement that can be
executed multiple times.
• It accepts parameterized SQL queries,
with ? as placeholders for parameters, which
can be set dynamically.
• Considering in the people database if there is a
need to INSERT some values,
• INSERT INTO people VALUES ("Ayan",25);
INSERT INTO people VALUES("Kriya",32);
• To do the same in Java, one may use Prepared
Statements and set the values in the ? holders,
• setXXX() of a prepared statement is used as
shown:
• String query = "INSERT INTO people(name,
age)VALUES(?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1,"Ayan");
ptstmt.setInt(2,25);
• Implementation: Once the PreparedStatement object is
created, there are three ways to execute it:
• execute(): This returns a boolean value and executes a
static SQL statement that is present in the prepared
statement object.
• executeQuery(): Returns a ResultSet from the current
prepared statement.
• executeUpdate(): Returns the number of rows affected
by the DML statements such as INSERT, DELETE, and
more that is present in the current Prepared Statement.
3. Callable Statement:
• A CallableStatement is used to execute stored
procedures in the database. Stored procedures
are precompiled SQL statements that can be
called with parameters.
• They are useful for executing complex
operations that involve multiple SQL
statements.
• Syntax: To create a CallableStatement,
• CallableStatement cstmt =
con.prepareCall("{call ProcedureName(?, ?)}");
• {call ProcedureName(?, ?)}: Calls a stored procedure
named ProcedureName with placeholders ? for input
parameters.
• Methods to Execute:
• execute(): Executes the stored procedure and returns
a boolean indicating whether the result is
a ResultSet (true) or an update count (false).
• executeQuery(): Executes a stored procedure that
returns a ResultSet.
• executeUpdate(): Executes a stored procedure that
performs an update and returns the number of rows
affected.