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

PPSX
JDBC: java DataBase connectivity
PPTX
SQLite database in android
PPTX
java interface and packages
PPT
SQLITE Android
PPSX
Data Types & Variables in JAVA
PDF
Basic Java Programming
PPTX
Introduction to php
JDBC: java DataBase connectivity
SQLite database in android
java interface and packages
SQLITE Android
Data Types & Variables in JAVA
Basic Java Programming
Introduction to php

What's hot (20)

PDF
PPTX
Applet programming
PPT
Swing and AWT in java
PDF
Object Oriented Analysis Design using UML
PPT
Java Networking
PDF
Layouts in android
PPT
Exception Handling in JAVA
PPTX
Asp.NET Validation controls
PPT
Core java concepts
PPTX
Applets in java
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPTX
ASP.NET Page Life Cycle
PPT
Introduction to .NET Framework
PPTX
Constructor in java
PPTX
Tcp/ip server sockets
PPTX
PPT
Java database connectivity
PPTX
Arrays in Java
Applet programming
Swing and AWT in java
Object Oriented Analysis Design using UML
Java Networking
Layouts in android
Exception Handling in JAVA
Asp.NET Validation controls
Core java concepts
Applets in java
Basic Concepts of OOPs (Object Oriented Programming in Java)
ASP.NET Page Life Cycle
Introduction to .NET Framework
Constructor in java
Tcp/ip server sockets
Java database connectivity
Arrays in Java
Ad

Similar to Computer Science:Java jdbc (20)

PPT
Jdbc slide for beginers
PPTX
Jdjdbcbc Jdjdbcbc JdjdbcJdjdbcbc Jdjdbcbc Jdjdbcbcbc JdJdbcbc
PPTX
creating jdbc connection
PPTX
creating jdbc connection
PPTX
Core jdbc basics
PPT
Jdbc ppt
PDF
JDBC-Introduction
PDF
Introduction to Java Database Connectivity (JDBC)
PPSX
Fundamentals of JDBC
PPT
Basic Java Database Connectivity(JDBC)
PPTX
5 things about introduction to advanced java you have to experience it yourself.
PPTX
Introduction to advanced java
PDF
JDBC with MySQL.pdf
PDF
JDBC with MySQL.pdf
PPTX
Java database connectivity with MySql
PPTX
Java database connectivity with MySql
PPTX
Java Database Connectivity by shreyash simu dbce.pptx
PPTX
Java Data Base Connectivity concepts.pptx
PPTX
JDBC PPT(4).pptx java Database Connectivity
Jdbc slide for beginers
Jdjdbcbc Jdjdbcbc JdjdbcJdjdbcbc Jdjdbcbc Jdjdbcbcbc JdJdbcbc
creating jdbc connection
creating jdbc connection
Core jdbc basics
Jdbc ppt
JDBC-Introduction
Introduction to Java Database Connectivity (JDBC)
Fundamentals of JDBC
Basic Java Database Connectivity(JDBC)
5 things about introduction to advanced java you have to experience it yourself.
Introduction to advanced java
JDBC with MySQL.pdf
JDBC with MySQL.pdf
Java database connectivity with MySql
Java database connectivity with MySql
Java Database Connectivity by shreyash simu dbce.pptx
Java Data Base Connectivity concepts.pptx
JDBC PPT(4).pptx java Database Connectivity
Ad

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

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

Recently uploaded (20)

PPTX
Pharma ospi slides which help in ospi learning
PPTX
Onica Farming 24rsclub profitable farm business
PDF
Insiders guide to clinical Medicine.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
From loneliness to social connection charting
PPTX
Cell Structure & Organelles in detailed.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Pharma ospi slides which help in ospi learning
Onica Farming 24rsclub profitable farm business
Insiders guide to clinical Medicine.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Module 3: Health Systems Tutorial Slides S2 2025
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Open Quiz Monsoon Mind Game Prelims.pptx
Introduction and Scope of Bichemistry.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
From loneliness to social connection charting
Cell Structure & Organelles in detailed.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
UPPER GASTRO INTESTINAL DISORDER.docx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Week 4 Term 3 Study Techniques revisited.pptx
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx

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