JDBCV 1
JDBCV 1
Sadhu Sreenivas
C -DAC Hyderabad
What is JDBC?
Definition
C -DAC Hyd
JDBC History
JDBC History
● Before JDBC, ODBC API was used to connect and execute query to
the database.
● But ODBC API uses ODBC driver that is written in C language which is
platform dependent and unsecured.
● Sun Micro System has defined its own API (JDBC API) that uses JDBC
driver written in Java language.
C -DAC Hyd
JDBC History
●
Some classes and interfaces are available javax.sql
C -DAC Hyd
What is API
API
The Java API is the set of classes included with the Java Development
● Environment. These classes are written using the Java language and
run on the JVM. The Java API includes everything from collection
classes to GUI classes.
●
JDBC is also an API.
C -DAC Hyd
JDBC Drivers
JDBC Drivers
C -DAC Hyd
Type 1:
JDBC-ODBC Bridge Driver
C -DAC Hyd
Type 2:
Native-API Driver
●
The Native API driver uses the client-side libraries of the database.
●
It is not written entirely in Java.
C -DAC Hyd
Type 3:
Network Protocol Driver
●
The Network Protocol driver uses middle ware (application server).
●
It is fully written in Java.
C -DAC Hyd
Type 4: Thin Driver
● The thin driver converts JDBC calls directly into the vendor-specific
database protocol.
● It is known as thin driver. It is fully written in Java
●
Better performance than all other drivers.
● No software is required at client side or server side.
● Disadvantage: Drivers depends on the Database.
C -DAC Hyd
Steps to Connect to the
Database in Java
Steps to Connect to
Database
There are 5 steps to connect any java application with the database
in java using JDBC. They are as follows:
●
Register the driver class
●
Creating connection
●
Creating statement
● Executing queries
● Closing connection
C -DAC Hyd
Registering the
Driver
C -DAC Hyd
Creating
Connection Object
C -DAC Hyd
Creating Statement
Object
C -DAC Hyd
Execute Query
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
C -DAC Hyd
Closing Connection
C -DAC Hyd
Connecting to the MySQL Database
Connect to MySQL
Database
●
Driver class: com.mysql.jdbc.Driver.
●
Connection URL: jdbc:mysql://localhost:3306/db_name
●
Username: The default username for the mysql database is root.
C -DAC Hyd
Loading the .jar
●
Download the MySQL connector.jar from mysql.com
C -DAC Hyd
DriverManager
class
C -DAC Hyd
Connection
Interface
●
A Connection is the session between Java application and database.
● The Connection interface is a factory of Statement and
PreparedStatement.
● Object of Connection can be used to get the object of Statement
and PreparedStatement.
C -DAC Hyd
Connection
Interface
C -DAC Hyd
Statement Interface
C -DAC Hyd
Statement Interface
C -DAC Hyd
Statement Interface
Example:
●
Statement stmt=con.createStatement();
int result=stmt.executeUpdate("delete from table where id=xy");
System.out.println(result+" records affected");
con.close();
C -DAC Hyd
ResultSet interface
C -DAC Hyd
ResultSet Interface
C -DAC Hyd
ResultSet Interface
C -DAC Hyd
ResultSet Interface
Example:
●
ResultSet rs=stmt.executeQuery("select * from table");
//getting the record of 3rd row
rs.absolute(3);
System.out.println(rs.getString(1)+" "+rs.getString(2)+"
"+rs.getString(3));
con.close();
C -DAC Hyd
PreparedStatement
Interface
●
The PreparedStatement interface is a sub interface of Statement.
●
It is used to execute parameterized query.
C -DAC Hyd
PreaparedStatement
Interface
Methods of PreparedStatement:
● public void setInt(int paramIndex, int value): sets the integer value
to the given parameter index.
● public void setString(int paramIndex, String value): sets the String
value to the given parameter index.
● public void setFloat(int paramIndex, float value): sets the float
value to the given parameter index.
C -DAC Hyd
PreparedStatement
Interface
Methods of PreparedStatement:
● public void setDouble(int paramIndex, double value): sets the
double value to the given parameter index.
● public int executeUpdate(): executes the query. It is used for
create, drop, insert, update, delete etc.
● public ResultSet executeQuery(): executes the select query. It
returns an instance of ResultSet.
C -DAC Hyd
PreparedStatement
Interface
Example:
● PreparedStatement stmt=con.prepareStatement("insert into Emp
values(?,?)");
stmt.setInt(1,101);//1 specifies the first parameter in the query
stmt.setString(2,”ABC");
int i=stmt.executeUpdate();
C -DAC Hyd
Questions
C -DAC Hyd
Thank You
C -DAC Hyd