SlideShare a Scribd company logo
2
Most read
7
Most read
11
Most read
JAVA JDBC
Seena. K
Assistant Professor
Department of Vocational Studies
St. Mary’s College, Thrissur
Java JDBC,Seena.k.,St.Mary’s College
Java 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.
“It is a Java-based data access technology used for Java database
connectivity.
 It provides methods to query and update data in a database, and is
oriented towards relational Databases
Java JDBC,Seena.k.,St.Mary’s College
JDBC Drivers
There are four types of JDBC drivers
1. JDBC-ODBC Bridge Driver,
2. Native Driver,
3. Network Protocol Driver, and Thin Driver
Java JDBC,Seena.k.,St.Mary’s College
 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.
Java JDBC,seena.K,St.Mary’s College
 We can use JDBC API to handle database using Java program and can perform
the following activities:
1. Connect to the database
2. Execute queries and update statements to the database
3. Retrieve the result received from the database.
 It is based on the X/Open SQL Call Level Interface.
 The java.sql package contains classes and interfaces for JDBC API
Java JDBC,Seena.k,St.Mar’ys College
Classes and Interfaces Of JDBC API
The java.sql package contains classes and interfaces for JDBC API. A
list of popular interfaces of JDBC API are given below:
Classes Interfaces
1. DriverManager class 1. Driver Interface
2. Blob class 2. Connection Interface
3. Clob class 3. Statement Interface
4. Types class 4. PreparedStatement Interface &
ResultSet Interface
Java JDBC,Seena.k,St.Mar’ys College
What is API
 API (Application programming interface) is a document that
contains a description of all the features of a product or software.
 It represents classes and interfaces that software programs can
follow to communicate with each other. An API can be created for
applications, libraries, operating systems,
Java JDBC,Seena.K,St.Mary’s College
Java Database Connectivity with 5 Steps
 There are 5 steps to connect any java application with the database
using JDBC. These steps are as follows:
1. Register the Driver class
2. Create connection
3. Create statement
4. Execute queries
5. Close connection
Java JDBC,Seena.K,St.Mary’s College
 The forName() method of class is used to register the driver class.
This method is used to dynamically load the driver 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.
Register the driver class
Java JDBC,Seena.K,St.Mary’s College
 A Connection is the session between java application and database. The
Connection interface is a factory of Statement, PreparedStatement, and
DatabaseMetaData i.e. object of Connection can be used to get the object
of Statement and DatabaseMetaData.
 public Statement createStatement(): creates a statement object that can
be used to execute SQL queries.
Connection interface
Java JDBC,Seena.K,St.Mary’s College
 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.
 public ResultSet executeQuery(String sql): is used to execute
SELECT query. It returns the object of ResultSet.
Statement interface
Java JDBC,Seena.K,St.Mary’s College
 The object of ResultSet maintains a cursor pointing to a row of a table.
Initially, cursor points to before the first row.
ResultSet Interface
public boolean next(): is used to move the cursor to the
one row next from the current
position.
3) public boolean first(): is used to move the cursor to the
first row in result set object.
4) public boolean last(): is used to move the cursor to the
last row in result set object.
Java JDBC,Seena.k,St.Mar’ys College
To connect Java application with the MySQL database, we need to follow
5 following steps.
 Driver class: The driver class for the mysql database
is com.mysql.jdbc.Driver.
 Connection URL: The connection URL for the mysql database
is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is
the database, localhost is the server name on which mysql is running,
we may also use IP address, 3306 is the port number.
Java Database Connectivity with MySQL
Java JDBC,Seena.k,St.Mar’ys College
Username: The default username for the mysql database is root.
Password: It is the password given by the user at the time of installing
the mysql database. In this example, we are going to use root as the
password.
Java Database Connectivity with MySQL
Java JDBC,Seena.k,St.Mar’ys College
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sonoo","root","root");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);} }}
Example to Connect Java Application with
mysql database
Java JDBC,Seena.k,St.Mar’ys College
REFERENCE
www.javatpoint.com

More Related Content

PPTX
Chapter 3 servlet & jsp
Jafar Nesargi
 
PPT
Servlets
Sasidhar Kothuru
 
PPT
Java Servlets
BG Java EE Course
 
PPTX
Jdbc ppt
sandeep54552
 
PPT
Jsp ppt
Vikas Jagtap
 
PPSX
JDBC: java DataBase connectivity
Tanmoy Barman
 
PPTX
java Servlet technology
Tanmoy Barman
 
PPT
Jdbc ppt
Vikas Jagtap
 
Chapter 3 servlet & jsp
Jafar Nesargi
 
Java Servlets
BG Java EE Course
 
Jdbc ppt
sandeep54552
 
Jsp ppt
Vikas Jagtap
 
JDBC: java DataBase connectivity
Tanmoy Barman
 
java Servlet technology
Tanmoy Barman
 
Jdbc ppt
Vikas Jagtap
 

What's hot (20)

PPT
JDBC – Java Database Connectivity
Information Technology
 
PPTX
servlet in java
sowfi
 
PPTX
Enterprise java unit-1_chapter-3
sandeep54552
 
PDF
Servlet and servlet life cycle
Dhruvin Nakrani
 
PPTX
JDBC ppt
Rohit Jain
 
PPTX
Java Spring Framework
Mehul Jariwala
 
PPTX
Java Server Pages
Kasun Madusanke
 
PPTX
jstl ( jsp standard tag library )
Adarsh Patel
 
PPTX
enterprise java bean
Jitender Singh Lodhi
 
PPTX
Session bean
sandeep54552
 
PPTX
Ajax
Tech_MX
 
PPTX
Servlets
ZainabNoorGul
 
ODP
OOP java
xball977
 
PPTX
Java servlets
yuvarani p
 
PPTX
Java database connectivity with MySql
Dhyey Dattani
 
PPTX
Java Server Pages(jsp)
Manisha Keim
 
PPTX
Servlet.ppt
VMahesh5
 
PPS
Jdbc architecture and driver types ppt
kamal kotecha
 
PPTX
Enterprise java unit-1_chapter-2
sandeep54552
 
JDBC – Java Database Connectivity
Information Technology
 
servlet in java
sowfi
 
Enterprise java unit-1_chapter-3
sandeep54552
 
Servlet and servlet life cycle
Dhruvin Nakrani
 
JDBC ppt
Rohit Jain
 
Java Spring Framework
Mehul Jariwala
 
Java Server Pages
Kasun Madusanke
 
jstl ( jsp standard tag library )
Adarsh Patel
 
enterprise java bean
Jitender Singh Lodhi
 
Session bean
sandeep54552
 
Ajax
Tech_MX
 
Servlets
ZainabNoorGul
 
OOP java
xball977
 
Java servlets
yuvarani p
 
Java database connectivity with MySql
Dhyey Dattani
 
Java Server Pages(jsp)
Manisha Keim
 
Servlet.ppt
VMahesh5
 
Jdbc architecture and driver types ppt
kamal kotecha
 
Enterprise java unit-1_chapter-2
sandeep54552
 
Ad

Similar to Computer Science:Java jdbc (20)

PPT
Jdbc slide for beginers
Ambarish Rai
 
PPTX
Jdjdbcbc Jdjdbcbc JdjdbcJdjdbcbc Jdjdbcbc Jdjdbcbcbc JdJdbcbc
rohanbawadkar
 
PPTX
creating jdbc connection
Paneliya Prince
 
PPTX
creating jdbc connection
Paneliya Prince
 
PPTX
Core jdbc basics
Sourabrata Mukherjee
 
PDF
JDBC-Introduction
Mythili Shankar
 
PDF
Introduction to Java Database Connectivity (JDBC)
Naresh IT
 
PPSX
Fundamentals of JDBC
Jainul Musani
 
PPT
Basic Java Database Connectivity(JDBC)
suraj pandey
 
PPTX
Introduction to advanced java
kritikumar16
 
PPTX
5 things about introduction to advanced java you have to experience it yourself.
kritikumar16
 
PDF
JDBC with MySQL.pdf
Arumugam90
 
PDF
JDBC with MySQL.pdf
Arumugam90
 
PPTX
Java database connectivity with MySql
Dhyey Dattani
 
PPTX
Java Database Connectivity by shreyash simu dbce.pptx
ash909077
 
PPTX
Java Data Base Connectivity concepts.pptx
mukeshprasanth909
 
PPTX
JDBC PPT(4).pptx java Database Connectivity
dhanushoneplus00
 
PPTX
435819601-Advanced-Java_Study About Java.pptx
JPrince9
 
Jdbc slide for beginers
Ambarish Rai
 
Jdjdbcbc Jdjdbcbc JdjdbcJdjdbcbc Jdjdbcbc Jdjdbcbcbc JdJdbcbc
rohanbawadkar
 
creating jdbc connection
Paneliya Prince
 
creating jdbc connection
Paneliya Prince
 
Core jdbc basics
Sourabrata Mukherjee
 
JDBC-Introduction
Mythili Shankar
 
Introduction to Java Database Connectivity (JDBC)
Naresh IT
 
Fundamentals of JDBC
Jainul Musani
 
Basic Java Database Connectivity(JDBC)
suraj pandey
 
Introduction to advanced java
kritikumar16
 
5 things about introduction to advanced java you have to experience it yourself.
kritikumar16
 
JDBC with MySQL.pdf
Arumugam90
 
JDBC with MySQL.pdf
Arumugam90
 
Java database connectivity with MySql
Dhyey Dattani
 
Java Database Connectivity by shreyash simu dbce.pptx
ash909077
 
Java Data Base Connectivity concepts.pptx
mukeshprasanth909
 
JDBC PPT(4).pptx java Database Connectivity
dhanushoneplus00
 
435819601-Advanced-Java_Study About Java.pptx
JPrince9
 
Ad

More from St Mary's College,Thrissur,Kerala (20)

PPTX
Creative writing and literature
St Mary's College,Thrissur,Kerala
 
PPTX
Online Data Storage
St Mary's College,Thrissur,Kerala
 
PPTX
Mathematics:Cryptography
St Mary's College,Thrissur,Kerala
 
PPTX
Mathematics:Arithmetical Functions
St Mary's College,Thrissur,Kerala
 
PPTX
Physical education :Yoga For Stress Relief
St Mary's College,Thrissur,Kerala
 
PPTX
Psychology:PAIN: Types, Theories and Assessment of pain
St Mary's College,Thrissur,Kerala
 
PPTX
Economics:Functions
St Mary's College,Thrissur,Kerala
 
PPTX
Mathematics:H-Complexity
St Mary's College,Thrissur,Kerala
 
PPTX
Statistics:Probability Theory
St Mary's College,Thrissur,Kerala
 
PPTX
Statistics:Fundamentals Of Statistics
St Mary's College,Thrissur,Kerala
 
PPTX
Economics:Public Revenue
St Mary's College,Thrissur,Kerala
 
PPTX
Economics:Public Debt
St Mary's College,Thrissur,Kerala
 
PPTX
Economics:Poverty-perspectives And Dimensions
St Mary's College,Thrissur,Kerala
 
PPTX
Economics:Economic Integration
St Mary's College,Thrissur,Kerala
 
PPTX
Economics:Enviornmental Pollution
St Mary's College,Thrissur,Kerala
 
PPTX
Computer Science:JavaScript
St Mary's College,Thrissur,Kerala
 
PPTX
Computer Science:Sql Set Operation
St Mary's College,Thrissur,Kerala
 
PPTX
Microbiology:General Principles of Food Preservation
St Mary's College,Thrissur,Kerala
 
PPTX
Microbiology:Induction Course-Introduction to Medical Microbiology
St Mary's College,Thrissur,Kerala
 
Creative writing and literature
St Mary's College,Thrissur,Kerala
 
Mathematics:Cryptography
St Mary's College,Thrissur,Kerala
 
Mathematics:Arithmetical Functions
St Mary's College,Thrissur,Kerala
 
Physical education :Yoga For Stress Relief
St Mary's College,Thrissur,Kerala
 
Psychology:PAIN: Types, Theories and Assessment of pain
St Mary's College,Thrissur,Kerala
 
Mathematics:H-Complexity
St Mary's College,Thrissur,Kerala
 
Statistics:Probability Theory
St Mary's College,Thrissur,Kerala
 
Statistics:Fundamentals Of Statistics
St Mary's College,Thrissur,Kerala
 
Economics:Public Revenue
St Mary's College,Thrissur,Kerala
 
Economics:Public Debt
St Mary's College,Thrissur,Kerala
 
Economics:Poverty-perspectives And Dimensions
St Mary's College,Thrissur,Kerala
 
Economics:Economic Integration
St Mary's College,Thrissur,Kerala
 
Economics:Enviornmental Pollution
St Mary's College,Thrissur,Kerala
 
Computer Science:JavaScript
St Mary's College,Thrissur,Kerala
 
Computer Science:Sql Set Operation
St Mary's College,Thrissur,Kerala
 
Microbiology:General Principles of Food Preservation
St Mary's College,Thrissur,Kerala
 
Microbiology:Induction Course-Introduction to Medical Microbiology
St Mary's College,Thrissur,Kerala
 

Recently uploaded (20)

PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PDF
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 

Computer Science:Java jdbc

  • 1. JAVA JDBC Seena. K Assistant Professor Department of Vocational Studies St. Mary’s College, Thrissur
  • 2. Java JDBC,Seena.k.,St.Mary’s College Java 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. “It is a Java-based data access technology used for Java database connectivity.  It provides methods to query and update data in a database, and is oriented towards relational Databases
  • 3. Java JDBC,Seena.k.,St.Mary’s College JDBC Drivers There are four types of JDBC drivers 1. JDBC-ODBC Bridge Driver, 2. Native Driver, 3. Network Protocol Driver, and Thin Driver
  • 4. Java JDBC,Seena.k.,St.Mary’s College  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.
  • 5. Java JDBC,seena.K,St.Mary’s College  We can use JDBC API to handle database using Java program and can perform the following activities: 1. Connect to the database 2. Execute queries and update statements to the database 3. Retrieve the result received from the database.  It is based on the X/Open SQL Call Level Interface.  The java.sql package contains classes and interfaces for JDBC API
  • 6. Java JDBC,Seena.k,St.Mar’ys College Classes and Interfaces Of JDBC API The java.sql package contains classes and interfaces for JDBC API. A list of popular interfaces of JDBC API are given below: Classes Interfaces 1. DriverManager class 1. Driver Interface 2. Blob class 2. Connection Interface 3. Clob class 3. Statement Interface 4. Types class 4. PreparedStatement Interface & ResultSet Interface
  • 7. Java JDBC,Seena.k,St.Mar’ys College What is API  API (Application programming interface) is a document that contains a description of all the features of a product or software.  It represents classes and interfaces that software programs can follow to communicate with each other. An API can be created for applications, libraries, operating systems,
  • 8. Java JDBC,Seena.K,St.Mary’s College Java Database Connectivity with 5 Steps  There are 5 steps to connect any java application with the database using JDBC. These steps are as follows: 1. Register the Driver class 2. Create connection 3. Create statement 4. Execute queries 5. Close connection
  • 9. Java JDBC,Seena.K,St.Mary’s College  The forName() method of class is used to register the driver class. This method is used to dynamically load the driver 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. Register the driver class
  • 10. Java JDBC,Seena.K,St.Mary’s College  A Connection is the session between java application and database. The Connection interface is a factory of Statement, PreparedStatement, and DatabaseMetaData i.e. object of Connection can be used to get the object of Statement and DatabaseMetaData.  public Statement createStatement(): creates a statement object that can be used to execute SQL queries. Connection interface
  • 11. Java JDBC,Seena.K,St.Mary’s College  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.  public ResultSet executeQuery(String sql): is used to execute SELECT query. It returns the object of ResultSet. Statement interface
  • 12. Java JDBC,Seena.K,St.Mary’s College  The object of ResultSet maintains a cursor pointing to a row of a table. Initially, cursor points to before the first row. ResultSet Interface public boolean next(): is used to move the cursor to the one row next from the current position. 3) public boolean first(): is used to move the cursor to the first row in result set object. 4) public boolean last(): is used to move the cursor to the last row in result set object.
  • 13. Java JDBC,Seena.k,St.Mar’ys College To connect Java application with the MySQL database, we need to follow 5 following steps.  Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.  Connection URL: The connection URL for the mysql database is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running, we may also use IP address, 3306 is the port number. Java Database Connectivity with MySQL
  • 14. Java JDBC,Seena.k,St.Mar’ys College Username: The default username for the mysql database is root. Password: It is the password given by the user at the time of installing the mysql database. In this example, we are going to use root as the password. Java Database Connectivity with MySQL
  • 15. Java JDBC,Seena.k,St.Mar’ys College import java.sql.*; class MysqlCon{ public static void main(String args[]){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/sonoo","root","root"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()) System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); con.close(); }catch(Exception e){ System.out.println(e);} }} Example to Connect Java Application with mysql database