Java JDBC (Unit-4)
Java JDBC (Unit-4)
Java JDBC (Unit-4)
KCS-602
by
❑ Transaction
❑ MetaData
JDBC (Java Database Connectivity)
• It’s a java API to connect & execute query with DB
• JDBC API uses JDBC drivers to connect with DB.
Why use JDBC
• Before JDBC, ODBC API was the database
API to connect and execute query with DB.
• But, ODBC API uses ODBC driver which is
written in C language (i.e. platform
dependent and unsecured).
Advantages:
• easy to use.
• can be easily connected to any DB.
Disadvantages:
• Performance degraded because JDBC method
call is converted into the ODBC function calls.
Advantage:
• performance upgraded than JDBC-ODBC
bridge driver.
Disadvantage:
• The Native driver needs to be installed on
the each client machine.
Disadvantages:
• Network support is required on client machine.
• Requires DB-specific coding to be done in middle
tier.
• Maintenance of Network Protocol driver becomes
costly because it requires DB-specific coding to be
done in the middle tier.
4) Type-4: Thin driver
Disadvantage:
• Drivers depends on the Database.
5 Steps to connect to the DB in java
1. Register the driver class
2. Creating connection
3. Creating statement
4. Executing queries
5. Closing connection
1) Register the driver class
• The forName() method of class Class is used to
register the driver class. It dynamically load the
driver class.
Class.forName("oracle.jdbc.driver.OracleDriver");
2) Create the connection object
• The getConnection() method of
DriverManager class is used to establish
connection with the DB.
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","pa
ssword");
3) Create the Statement object
• The createStatement() method of Connection
interface is used to create statement.
• The object of statement is responsible to execute
SQL & return the results by using the ResultSet
object.
Improves performance:
- can be used to represent a precompiled query,
which can be executed multiple times.
Disadvantage:
It can represent only one SQL statement at a time.
Example of PreparedStatement interface that inserts the record
import java.sql.*;
public class insertPST {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/kiet","root","");
String query="insert into student values(?,?)";
PreparedStatement pst=con.prepareStatement(query);
pst.setInt(1,103);
pst.setString(2, "Bhanu");
int r=pst.executeUpdate();
System.out.println(r+"\t Rows Inserted Successfully!!!");
ResultSet rs = pst.executeQuery("select * from student");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2));
con.close();
}catch(Exception e) {
System.out.println(e);}
}}
Example of PreparedStatement interface that updates/deletes/retrieves the record
String query="update student set sname=? where sid=?";
PreparedStatement pst=con.prepareStatement(query);
pst.setInt(2,103);
pst.setString(1, "Bhavya");
int r=pst.executeUpdate();
System.out.println(r+"\t Rows Updated Successfully!!!");
CallableStatement cs=con.prepareCall("{call
Proc_Name(?,?)}");
Super interface for Prepared and Callable extends Statement (sub- extends PreparedStatement (sub-
Statement interface) interface)
PreparedStatement CallableStatement
ps=con.prepareStatement cs=conn.prepareCall("{call
stmt = conn.createStatement(); ("insert into studentDiet
values(?,?,?)"); getbranch(?,?)}");
• used for reading content of • used for altering the DBs • If you dont know which
DB • Statement: DROP, INSERT, method to be used for
UPDATE, DELETE used. executing SQL statements,
this method can be used.
• The output will be in the form
of ResultSet. • Output will be in the form of • This will return a boolean.
int.
• Generally SELECT statement • This int value denotes the • TRUE indicates the result is a
is used. number of rows affected by ResultSet and FALSE indicates
the query. it has the int value which
denotes number of rows
affected by the query.
Method Semantics
first() Moves cursor to first row
last() Moves cursor to last row
next() Moves cursor to next row
previous() Moves cursor to just before the first row
afterLast() Moves cursor to just after the last row
For example:
◼ rs.getString(“sname”):
returns the student name as a String
import java.sql.*;
public class databaseMD {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/kiet","root","");
DatabaseMetaData dbmd=con.getMetaData();
System.out.println ("getDatabaseProductName:"+dbmd.getDatabaseProductName());
System.out.println("getDatabaseProductVersion():"+dbmd.getDatabaseProductVersion());
System.out.println("getDriverName():"+dbmd.getDriverName());
System.out.println("getDriverVersion():"+dbmd.getDriverVersion());
System.out.println("getURL():"+dbmd.getURL());
System.out.println("getUserName():"+dbmd.getUserName());
con.close();
}catch(Exception e)
{
System.out.println(e);
}}}
OUTPUT:
Thank
You