0% found this document useful (0 votes)
12 views

JDBCV 1

Uploaded by

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

JDBCV 1

Uploaded by

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

JDBC

Sadhu Sreenivas

C -DAC Hyderabad
What is JDBC?
Definition

● JDBC is a Java-based data access technology (Java Standard Edition


platform) from Oracle Corporation.
● This technology is an API for the Java programming language that
defines how a client may access a database.

It provides methods for querying and updating data in a database.

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

● Sun Microsystems released JDBC as part of JDK 1.1 on February 19,


1997.
● The JDBC classes are contained in the Java package java.sql


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

● JDBC Driver is a software component that enables java application


to interact with the database.There are 4 types of JDBC drivers:
– Type 1: JDBC-ODBC bridge driver
– Type 2: Native-API driver (partially java driver)

– Type 3: Network Protocol driver (fully java driver)

– Type 4: Thin driver (fully java driver)

C -DAC Hyd
Type 1:
JDBC-ODBC Bridge Driver

● The JDBC-ODBC bridge driver uses ODBC driver to connect to the


database. This is now discouraged because of thin 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

● The forName() method of Class class is used to register the driver


class.

Class.forName("com.mysql.jdbc.Driver");

C -DAC Hyd
Creating
Connection Object

● The getConnection() method of DriverManager class is used to


establish connection with the database.

Connection
con=DriverManager.getConnection( "jdbc:mysql://localhost:3306","r
oot","password");

C -DAC Hyd
Creating 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.

Statement stmt=con.createStatement();

C -DAC Hyd
Execute Query

● 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.

ResultSet rs=stmt.executeQuery("select * from emp");

while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));

C -DAC Hyd
Closing Connection

● By closing connection object statement and ResultSet will be closed


automatically. The close() method of Connection interface is used to
close the connection.

con.close();

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.

● Password: Given by the user at the time of installing the mysql


database
● Example:
Connection
con=DriverManager.getConnection( “jdbc:mysql://
localhost:3306/cdac","root","admin");

C -DAC Hyd
Loading the .jar


Download the MySQL connector.jar from mysql.com

Paste the mysqlconnector.jar in the lib folder of source directory.


Set the classpath

C -DAC Hyd
DriverManager
class

● The DriverManager class acts as an interface between user and


drivers.
● It keeps track of the drivers that are available and handles
establishing a connection between a database and the appropriate
driver.

Connection con = null;
con=DriverManager.getConnection( "jdbc:mysql://localhost:3306","r
oot","password");

DriverManager.registerDriver().

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

Methods of Connection interface:


● public Statement createStatement(): creates a statement object
that can be used to execute SQL queries.
● public void commit(): saves the changes made since the previous
commit/rollback permanent.
● public void close(): closes the connection and Releases a JDBC
resources immediately.

C -DAC Hyd
Statement Interface

● The Statement interface provides methods to execute queries with


the database.

The statement interface is a factory of ResultSet.

It provides factory method to get the object of ResultSet.

C -DAC Hyd
Statement Interface

Methods of Statement interface:


● public ResultSet executeQuery(String sql): is used to execute SELECT
query. It returns the object of ResultSet.
● public int executeUpdate(String sql): is used to execute specified
query, it may be create, drop, insert, update, delete etc.
● public boolean execute(String sql): is used to execute queries that
may return multiple results.

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

● The object of ResultSet maintains a cursor pointing to a particular


row of data.

Initially, cursor points to before the first row.

C -DAC Hyd
ResultSet Interface

Methods of ResultSet interface:


● public boolean next(): is used to move the cursor to the one row
next from the current position.
● public boolean previous():is used to move the cursor to the one
row previous from the current position.
● public boolean first(): is used to move the cursor to the first row
in result set object.
● public boolean last():is used to move the cursor to the last row in
result set object.

C -DAC Hyd
ResultSet Interface

Methods of ResultSet interface:


● public int getInt(int columnIndex): is used to return the data of
specified column index of the current row as int.
● public int getInt(String columnName): columnName): is used to
return the data of specified column name of the current row as int.
● public String getString(int columnIndex): is used to return the data
of specified column index of the current row as String.
● public String getString(String columnName): is used to return the
data of specified column name of the current row as String.

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.

● Example of parameterized query:


– String sql="insert into emp values(?,?,?)";

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();

System.out.println(i+" records inserted");

C -DAC Hyd
Questions

Write a program to implement CRUD in a table.

Write a program to implement PreparedStatement?

C -DAC Hyd
Thank You

C -DAC Hyd

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy