SlideShare a Scribd company logo
Advance Java Programming(22517)
Miss.P.S.Dungarwal
Lecturer in CM Department.
SHHJB Polytechnic, Chandwad.
ODBC stands for Open Database
Connectivity
A standard or open application
programming interface (API) for
accessing a database.
ODBC provides a C interface for
database access on Windows
environment.
2Miss.P.S.Dungarwal
JDBC stands
for Java Database Connectivity.
It is a standard Java API for
connecting programs written in
Java to the data in relational
databases.
JDBC works with Java on a variety
of platforms, such as Windows, Mac
OS, and the various versions of
UNIX.
3Miss.P.S.Dungarwal
 JDBC Driver is a software component that
enables java application to interact with the
database. There are 4 types of JDBC drivers:
 JDBC-ODBC bridge driver
 Native-API driver (partially java driver)
 JDBC-Net pure Java/ Network-Protocol driver (fully
java driver)
 Pure Java Driver /Thin driver / Database-Protocol
driver(fully java driver)
4Miss.P.S.Dungarwal
 The JDBC type 1 driver, also known as the JDBC-
ODBC bridge driver.
 The JDBC-ODBC bridge driver uses ODBC driver to
connect to the database. The JDBC-ODBC bridge
driver converts JDBC method calls into the ODBC
function calls.
5Miss.P.S.Dungarwal
 The JDBC type 2 driver, also known as
the Native-API driver
 The Native API driver uses the client-side
libraries of the database. The driver converts
JDBC method calls into native calls of the
database API. It is not written entirely in java.
6Miss.P.S.Dungarwal
 The JDBC type 3 driver, also known as the Pure Java
driver for database middleware. It is a database
driver implementation which makes use of a middle
tier between the calling program and the database.
 The middle-tier (application server)
converts JDBC calls directly or indirectly into a
vendor-specific database protocol. It is fully written
in java.
7Miss.P.S.Dungarwal
 The JDBC type 4 driver, also known as the Direct to
Database Pure Java Driver, is a database driver
implementation that converts JDBC calls directly into a
vendor specific database protocol.
 That is why it is known as thin driver. It is fully written in
Java language.
8Miss.P.S.Dungarwal
 In a two-tier model, a Java application
communicates directly with the database, via
the JDBC driver.
9Miss.P.S.Dungarwal
 In a three-tier model, a Java application
communicates with a middle tier component
that functions as an application server. The
application server talks to a given database
using JDBC.
10Miss.P.S.Dungarwal
 The JDBC API provides the following
interfaces and classes −
 DriverManager Class
 Driver Interface
 Connection Interface
 Statement Interface
 ResultSet Interface
11Miss.P.S.Dungarwal
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.
 The DriverManager class maintains a list of
Driver classes that have registered themselves
by calling the method
DriverManager.registerDriver().
12Miss.P.S.Dungarwal
Method Description
public static void registerDriver(
Driver driver);
is used to register the given driver
with DriverManager.
public static void deregisterDriver(
Driver driver);
is used to deregister the given
driver (drop the driver from the
list) with DriverManager.
public static Connection
getConnection ( String url);
is used to establish the connection
with the specified url.
public static Connection
getConnection( String url, String
userName, String password);
is used to establish the connection
with the specified url, username
and password.
Commonly used methods of DriverManager class
13Miss.P.S.Dungarwal
 This interface handles the communications
with the database server.
 You will very rarely interact directly with Driver
objects.
 Instead, you use DriverManager objects, which
manages objects of this type.
14Miss.P.S.Dungarwal
 A Connection is the session between java application
and database.
 The Connection interface provide many methods for
transaction management like commit(), rollback() etc.
 When getConnection() method is called, it returns a
connection object.
Connection
con=DriverManager.getConnection(URL);
15Miss.P.S.Dungarwal
Commonly used methods of Connection
interface
Method Description
public Statement
createStatement();
creates a statement
object that can be used
to execute SQL queries.
public void
setAutoCommit(bool
ean status);
It is used to set the
commit status. By
default it is true.
public void
commit();
It saves the changes
made since the previous
commit/rollback
permanent.
public void
rollback();
Drops all changes made
since the previous
commit/rollback.
public void close();
closes the connection and
Releases a JDBC resources
immediately.
16Miss.P.S.Dungarwal
 The Statement interface provides methods to
execute queries with the database.
 It provides factory method to get the object of
ResultSet.
17Miss.P.S.Dungarwal
Method Description
public ResultSet
executeQuery(String
sql);
used to execute SELECT query.
It returns the object of ResultSet.
public int
executeUpdate(String
sql);
used to execute specified query,
it may be create, drop, insert,
update, delete etc.
public boolean
execute(String sql);
used to execute queries that may
return multiple results.
public int[]
executeBatch();
used to execute batch of
commands.
void close() Close the statement object
Commonly used methods of Statement interface
18Miss.P.S.Dungarwal
 A ResultSet object provides access to a table
of data.
 ResultSet object is usually generated by
executing a statement.
 The object of ResultSet maintains a cursor
pointing to a particular row of data.
 Initially, cursor points before the first row.
19Miss.P.S.Dungarwal
Method Description
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.
public boolean absolute(int
row);
is used to move the cursor to the specified row
number in the ResultSet object.
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);
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.
Commonly used methods of ResultSet interface
20Miss.P.S.Dungarwal
 There are 5 steps to connect any java
application with the database in java using
JDBC. They are as follows:
1. Register the driver class
2. Creating connection
3. Creating statement
4. Executing queries
5. Closing connection
21Miss.P.S.Dungarwal
 The Class.forName() method is used to register the
driver class. This method is used to dynamically load
the driver class.
 Syntax of forName() method
public static void forName(String className)throws ClassNotFoun
dException
 Example to register with JDBC-ODBC Driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
22Miss.P.S.Dungarwal
2. Creating connection
 The DriverManager.getConnection() method is
used to establish connection with the database.
Example establish connection with
Oracle Driver
Connection con = DriverManager.getConnection
("jdbc:odbc:DemoDB","username","password");
23Miss.P.S.Dungarwal
3. Creating statement
 The createStatement() method of
Connection interface is used to create
statement. The object of statement is
responsible to execute queries with the
database.
 Example to create the statement object
Statement stmt=con.createStatement();
24Miss.P.S.Dungarwal
4. Executing queries
 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.
 Example to execute query
ResultSet rs=stmt.executeQuery("select * from emp"
);
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(
2));
} 25Miss.P.S.Dungarwal
5. 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.
 Example to close connection
con.close();
26Miss.P.S.Dungarwal
Example to Connect Java
Application with mysql database
27Miss.P.S.Dungarwal
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:33
06/Emp","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); }
} 28Miss.P.S.Dungarwal
29Miss.P.S.Dungarwal
import java.sql.*;
public class JdbcAccessTest {
public static void main(String[] args) {
String databaseURL = "jdbc:ucanaccess://e://Contacts.accdb";
try (
Connection connection = DriverManager.getConnection(databaseURL)) {
String sql = "INSERT INTO Contacts (Full_Name, Email, Phone) VALUES
(?, ?, ?)";
PreparedStatement preparedStatement =
connection.prepareStatement(sql);
preparedStatement.setString(1, "Rohit");
preparedStatement.setString(2, "rohit@mi.com");
preparedStatement.setString(3, "0919989998");
int row = preparedStatement.executeUpdate();
if (row > 0) {
System.out.println("A row has been inserted successfully.");
}
30Miss.P.S.Dungarwal
sql = "SELECT * FROM Contacts";
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);
while (result.next()) {
int id = result.getInt("Contact_ID");
String fullname = result.getString("Full_Name");
String email = result.getString("Email");
String phone = result.getString("Phone");
System.out.println(id + ", " + fullname + ", " + email + ", " +
phone);
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
31Miss.P.S.Dungarwal

More Related Content

What's hot

Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
Google
 
Java swing
Java swingJava swing
Java swing
Apurbo Datta
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
Ajax ppt
Ajax pptAjax ppt
Presentation on Core java
Presentation on Core javaPresentation on Core java
Presentation on Core java
mahir jain
 
Servlets
ServletsServlets
Servlets
ZainabNoorGul
 
Snake game powerpoint presentation by rohit malav
Snake game powerpoint presentation by rohit malavSnake game powerpoint presentation by rohit malav
Snake game powerpoint presentation by rohit malav
Rohit malav
 
Controls
ControlsControls
Controls
teach4uin
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
Arulmurugan Rajaraman
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBC
FAKHRUN NISHA
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Authentication and Authorization in Asp.Net
Authentication and Authorization in Asp.NetAuthentication and Authorization in Asp.Net
Authentication and Authorization in Asp.Net
Shivanand Arur
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
kamal kotecha
 
android layouts
android layoutsandroid layouts
android layoutsDeepa Rani
 
Angularjs PPT
Angularjs PPTAngularjs PPT
Angularjs PPT
Amit Baghel
 

What's hot (20)

Applets in java
Applets in javaApplets in java
Applets in java
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Vb.net ide
Vb.net ideVb.net ide
Vb.net ide
 
Java swing
Java swingJava swing
Java swing
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Ajax ppt
Ajax pptAjax ppt
Ajax ppt
 
Presentation on Core java
Presentation on Core javaPresentation on Core java
Presentation on Core java
 
Js ppt
Js pptJs ppt
Js ppt
 
Servlets
ServletsServlets
Servlets
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
 
Snake game powerpoint presentation by rohit malav
Snake game powerpoint presentation by rohit malavSnake game powerpoint presentation by rohit malav
Snake game powerpoint presentation by rohit malav
 
Controls
ControlsControls
Controls
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBC
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
Authentication and Authorization in Asp.Net
Authentication and Authorization in Asp.NetAuthentication and Authorization in Asp.Net
Authentication and Authorization in Asp.Net
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Android Report
Android ReportAndroid Report
Android Report
 
android layouts
android layoutsandroid layouts
android layouts
 
Angularjs PPT
Angularjs PPTAngularjs PPT
Angularjs PPT
 

Similar to Advance Java Programming (CM5I)5.Interacting with-database

Jdbc
JdbcJdbc
Database connect
Database connectDatabase connect
Database connect
Yoga Raja
 
Overview Of JDBC
Overview Of JDBCOverview Of JDBC
Overview Of JDBC
Mindfire Solutions
 
Jdbc
JdbcJdbc
Introduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsIntroduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applications
Fulvio Corno
 
Core jdbc basics
Core jdbc basicsCore jdbc basics
Core jdbc basics
Sourabrata Mukherjee
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
Arati Gadgil
 
Unit 5.pdf
Unit 5.pdfUnit 5.pdf
Unit 5.pdf
saturo3011
 
Final Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptFinal Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.ppt
TabassumMaktum
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivitybackdoor
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
DrMeenakshiS
 
Jdbc sasidhar
Jdbc  sasidharJdbc  sasidhar
Jdbc sasidhar
Sasidhar Kothuru
 
JDBC Part - 2
JDBC Part - 2JDBC Part - 2
JDBC Part - 2
Hitesh-Java
 

Similar to Advance Java Programming (CM5I)5.Interacting with-database (20)

Jdbc
JdbcJdbc
Jdbc
 
Database connect
Database connectDatabase connect
Database connect
 
Jdbc 1
Jdbc 1Jdbc 1
Jdbc 1
 
Overview Of JDBC
Overview Of JDBCOverview Of JDBC
Overview Of JDBC
 
Jdbc
JdbcJdbc
Jdbc
 
Introduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applicationsIntroduction to JDBC and database access in web applications
Introduction to JDBC and database access in web applications
 
Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
 
JDBC programming
JDBC programmingJDBC programming
JDBC programming
 
Core jdbc basics
Core jdbc basicsCore jdbc basics
Core jdbc basics
 
JDBC.ppt
JDBC.pptJDBC.ppt
JDBC.ppt
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
 
Unit 5.pdf
Unit 5.pdfUnit 5.pdf
Unit 5.pdf
 
Jdbc
JdbcJdbc
Jdbc
 
Final Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptFinal Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.ppt
 
Java Database Connectivity
Java Database ConnectivityJava Database Connectivity
Java Database Connectivity
 
JDBC
JDBCJDBC
JDBC
 
Jdbc introduction
Jdbc introductionJdbc introduction
Jdbc introduction
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
 
Jdbc sasidhar
Jdbc  sasidharJdbc  sasidhar
Jdbc sasidhar
 
JDBC Part - 2
JDBC Part - 2JDBC Part - 2
JDBC Part - 2
 

Recently uploaded

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 

Recently uploaded (20)

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 

Advance Java Programming (CM5I)5.Interacting with-database

  • 1. Advance Java Programming(22517) Miss.P.S.Dungarwal Lecturer in CM Department. SHHJB Polytechnic, Chandwad.
  • 2. ODBC stands for Open Database Connectivity A standard or open application programming interface (API) for accessing a database. ODBC provides a C interface for database access on Windows environment. 2Miss.P.S.Dungarwal
  • 3. JDBC stands for Java Database Connectivity. It is a standard Java API for connecting programs written in Java to the data in relational databases. JDBC works with Java on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. 3Miss.P.S.Dungarwal
  • 4.  JDBC Driver is a software component that enables java application to interact with the database. There are 4 types of JDBC drivers:  JDBC-ODBC bridge driver  Native-API driver (partially java driver)  JDBC-Net pure Java/ Network-Protocol driver (fully java driver)  Pure Java Driver /Thin driver / Database-Protocol driver(fully java driver) 4Miss.P.S.Dungarwal
  • 5.  The JDBC type 1 driver, also known as the JDBC- ODBC bridge driver.  The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function calls. 5Miss.P.S.Dungarwal
  • 6.  The JDBC type 2 driver, also known as the Native-API driver  The Native API driver uses the client-side libraries of the database. The driver converts JDBC method calls into native calls of the database API. It is not written entirely in java. 6Miss.P.S.Dungarwal
  • 7.  The JDBC type 3 driver, also known as the Pure Java driver for database middleware. It is a database driver implementation which makes use of a middle tier between the calling program and the database.  The middle-tier (application server) converts JDBC calls directly or indirectly into a vendor-specific database protocol. It is fully written in java. 7Miss.P.S.Dungarwal
  • 8.  The JDBC type 4 driver, also known as the Direct to Database Pure Java Driver, is a database driver implementation that converts JDBC calls directly into a vendor specific database protocol.  That is why it is known as thin driver. It is fully written in Java language. 8Miss.P.S.Dungarwal
  • 9.  In a two-tier model, a Java application communicates directly with the database, via the JDBC driver. 9Miss.P.S.Dungarwal
  • 10.  In a three-tier model, a Java application communicates with a middle tier component that functions as an application server. The application server talks to a given database using JDBC. 10Miss.P.S.Dungarwal
  • 11.  The JDBC API provides the following interfaces and classes −  DriverManager Class  Driver Interface  Connection Interface  Statement Interface  ResultSet Interface 11Miss.P.S.Dungarwal
  • 12. 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.  The DriverManager class maintains a list of Driver classes that have registered themselves by calling the method DriverManager.registerDriver(). 12Miss.P.S.Dungarwal
  • 13. Method Description public static void registerDriver( Driver driver); is used to register the given driver with DriverManager. public static void deregisterDriver( Driver driver); is used to deregister the given driver (drop the driver from the list) with DriverManager. public static Connection getConnection ( String url); is used to establish the connection with the specified url. public static Connection getConnection( String url, String userName, String password); is used to establish the connection with the specified url, username and password. Commonly used methods of DriverManager class 13Miss.P.S.Dungarwal
  • 14.  This interface handles the communications with the database server.  You will very rarely interact directly with Driver objects.  Instead, you use DriverManager objects, which manages objects of this type. 14Miss.P.S.Dungarwal
  • 15.  A Connection is the session between java application and database.  The Connection interface provide many methods for transaction management like commit(), rollback() etc.  When getConnection() method is called, it returns a connection object. Connection con=DriverManager.getConnection(URL); 15Miss.P.S.Dungarwal
  • 16. Commonly used methods of Connection interface Method Description public Statement createStatement(); creates a statement object that can be used to execute SQL queries. public void setAutoCommit(bool ean status); It is used to set the commit status. By default it is true. public void commit(); It saves the changes made since the previous commit/rollback permanent. public void rollback(); Drops all changes made since the previous commit/rollback. public void close(); closes the connection and Releases a JDBC resources immediately. 16Miss.P.S.Dungarwal
  • 17.  The Statement interface provides methods to execute queries with the database.  It provides factory method to get the object of ResultSet. 17Miss.P.S.Dungarwal
  • 18. Method Description public ResultSet executeQuery(String sql); used to execute SELECT query. It returns the object of ResultSet. public int executeUpdate(String sql); used to execute specified query, it may be create, drop, insert, update, delete etc. public boolean execute(String sql); used to execute queries that may return multiple results. public int[] executeBatch(); used to execute batch of commands. void close() Close the statement object Commonly used methods of Statement interface 18Miss.P.S.Dungarwal
  • 19.  A ResultSet object provides access to a table of data.  ResultSet object is usually generated by executing a statement.  The object of ResultSet maintains a cursor pointing to a particular row of data.  Initially, cursor points before the first row. 19Miss.P.S.Dungarwal
  • 20. Method Description 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. public boolean absolute(int row); is used to move the cursor to the specified row number in the ResultSet object. 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); 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. Commonly used methods of ResultSet interface 20Miss.P.S.Dungarwal
  • 21.  There are 5 steps to connect any java application with the database in java using JDBC. They are as follows: 1. Register the driver class 2. Creating connection 3. Creating statement 4. Executing queries 5. Closing connection 21Miss.P.S.Dungarwal
  • 22.  The Class.forName() method is used to register the driver class. This method is used to dynamically load the driver class.  Syntax of forName() method public static void forName(String className)throws ClassNotFoun dException  Example to register with JDBC-ODBC Driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 22Miss.P.S.Dungarwal
  • 23. 2. Creating connection  The DriverManager.getConnection() method is used to establish connection with the database. Example establish connection with Oracle Driver Connection con = DriverManager.getConnection ("jdbc:odbc:DemoDB","username","password"); 23Miss.P.S.Dungarwal
  • 24. 3. Creating statement  The createStatement() method of Connection interface is used to create statement. The object of statement is responsible to execute queries with the database.  Example to create the statement object Statement stmt=con.createStatement(); 24Miss.P.S.Dungarwal
  • 25. 4. Executing queries  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.  Example to execute query ResultSet rs=stmt.executeQuery("select * from emp" ); while(rs.next()) { System.out.println(rs.getInt(1)+" "+rs.getString( 2)); } 25Miss.P.S.Dungarwal
  • 26. 5. 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.  Example to close connection con.close(); 26Miss.P.S.Dungarwal
  • 27. Example to Connect Java Application with mysql database 27Miss.P.S.Dungarwal
  • 28. 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:33 06/Emp","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); } } 28Miss.P.S.Dungarwal
  • 30. import java.sql.*; public class JdbcAccessTest { public static void main(String[] args) { String databaseURL = "jdbc:ucanaccess://e://Contacts.accdb"; try ( Connection connection = DriverManager.getConnection(databaseURL)) { String sql = "INSERT INTO Contacts (Full_Name, Email, Phone) VALUES (?, ?, ?)"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, "Rohit"); preparedStatement.setString(2, "rohit@mi.com"); preparedStatement.setString(3, "0919989998"); int row = preparedStatement.executeUpdate(); if (row > 0) { System.out.println("A row has been inserted successfully."); } 30Miss.P.S.Dungarwal
  • 31. sql = "SELECT * FROM Contacts"; Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(sql); while (result.next()) { int id = result.getInt("Contact_ID"); String fullname = result.getString("Full_Name"); String email = result.getString("Email"); String phone = result.getString("Phone"); System.out.println(id + ", " + fullname + ", " + email + ", " + phone); } } catch (SQLException ex) { ex.printStackTrace(); } } } 31Miss.P.S.Dungarwal