SlideShare a Scribd company logo
1 of 35
Download to read offline
www.webstackacademy.com
JDBC
www.webstackacademy.com
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.
www.webstackacademy.com
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.
● That is why Sun Micro System has defined its own API (JDBC API)
that uses JDBC driver written in Java language.
www.webstackacademy.com
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 and
javax.sql
● The latest version, JDBC 4.2, and is included in Java SE 8.
www.webstackacademy.com
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.
www.webstackacademy.com
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)
www.webstackacademy.com
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.
www.webstackacademy.com
Type 2:
Native-API Driver
● The Native API driver uses the client-side libraries of the database.
● It is not written entirely in Java.
www.webstackacademy.com
Type 3:
Network Protocol Driver
● The Network Protocol driver uses middle ware (application server).
● It is fully written in Java.
www.webstackacademy.com
Type 4: Thin Driver
● The thin driver converts JDBC calls directly into the vendor-specific
database protocol.
● That is why it is known as thin driver.
● It is fully written in Java language.
● Better performance than all other drivers.
● No software is required at client side or server side.
● Disadvantage: Drivers depends on the Database.
www.webstackacademy.com
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
www.webstackacademy.com
Registering the
Driver
● The forName() method of Class class is used to register the driver
class.
●
Class.forName("com.mysql.jdbc.Driver");
www.webstackacademy.com
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","roo
t","password");
www.webstackacademy.com
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();
www.webstackacademy.com
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));
}
www.webstackacademy.com
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();
www.webstackacademy.com
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/sonoo","root","root");
www.webstackacademy.com
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
www.webstackacademy.com
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","roo
t","password");
● DriverManager.registerDriver().
www.webstackacademy.com
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.
www.webstackacademy.com
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.
www.webstackacademy.com
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.
www.webstackacademy.com
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.
www.webstackacademy.com
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();
www.webstackacademy.com
ResultSet interface
● The object of ResultSet maintains a cursor pointing to a particular row
of data.
● Initially, cursor points to before the first row.
www.webstackacademy.com
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.
www.webstackacademy.com
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.
www.webstackacademy.com
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();
www.webstackacademy.com
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(?,?,?)";
www.webstackacademy.com
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.
www.webstackacademy.com
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.
www.webstackacademy.com
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,"Ratan");
int i=stmt.executeUpdate();
System.out.println(i+" records inserted");
www.webstackacademy.com
Questions
Write a program to implement CRUD in a table.
Write a program to implement PreparedStatement?
Web Stack Academy (P) Ltd
#83, Farah Towers,
1st floor,MG Road,
Bangalore – 560001
M: +91-80-4128 9576
T: +91-98862 69112
E: info@www.webstackacademy.com
www.webstackacademy.com

More Related Content

What's hot

30 5 Database Jdbc
30 5 Database Jdbc30 5 Database Jdbc
30 5 Database Jdbcphanleson
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commandsphanleson
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09Terry Yoast
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08Terry Yoast
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slidesmetsarin
 
Sql injection with sqlmap
Sql injection with sqlmapSql injection with sqlmap
Sql injection with sqlmapHerman Duarte
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An IntroductionSmita Prasad
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10Terry Yoast
 
SQLMAP Tool Usage - A Heads Up
SQLMAP Tool Usage - A  Heads UpSQLMAP Tool Usage - A  Heads Up
SQLMAP Tool Usage - A Heads UpMindfire Solutions
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHPTaha Malampatti
 
Performance schema in_my_sql_5.6_pluk2013
Performance schema in_my_sql_5.6_pluk2013Performance schema in_my_sql_5.6_pluk2013
Performance schema in_my_sql_5.6_pluk2013Valeriy Kravchuk
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06Terry Yoast
 

What's hot (20)

30 5 Database Jdbc
30 5 Database Jdbc30 5 Database Jdbc
30 5 Database Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Executing Sql Commands
Executing Sql CommandsExecuting Sql Commands
Executing Sql Commands
 
Jdbc tutorial
Jdbc tutorialJdbc tutorial
Jdbc tutorial
 
9780538745840 ppt ch09
9780538745840 ppt ch099780538745840 ppt ch09
9780538745840 ppt ch09
 
Lecture17
Lecture17Lecture17
Lecture17
 
Jdbc sasidhar
Jdbc  sasidharJdbc  sasidhar
Jdbc sasidhar
 
9780538745840 ppt ch08
9780538745840 ppt ch089780538745840 ppt ch08
9780538745840 ppt ch08
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
Sql injection with sqlmap
Sql injection with sqlmapSql injection with sqlmap
Sql injection with sqlmap
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An Introduction
 
9780538745840 ppt ch10
9780538745840 ppt ch109780538745840 ppt ch10
9780538745840 ppt ch10
 
SQLMAP Tool Usage - A Heads Up
SQLMAP Tool Usage - A  Heads UpSQLMAP Tool Usage - A  Heads Up
SQLMAP Tool Usage - A Heads Up
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Performance schema in_my_sql_5.6_pluk2013
Performance schema in_my_sql_5.6_pluk2013Performance schema in_my_sql_5.6_pluk2013
Performance schema in_my_sql_5.6_pluk2013
 
PHP with MySQL
PHP with MySQLPHP with MySQL
PHP with MySQL
 
9780538745840 ppt ch06
9780538745840 ppt ch069780538745840 ppt ch06
9780538745840 ppt ch06
 
spring-tutorial
spring-tutorialspring-tutorial
spring-tutorial
 
Jdbc day-1
Jdbc day-1Jdbc day-1
Jdbc day-1
 

Similar to Core Java Programming Language (JSE) : Chapter XIII - JDBC

Similar to Core Java Programming Language (JSE) : Chapter XIII - JDBC (20)

Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet Solution
 
Jdbc
JdbcJdbc
Jdbc
 
Database connect
Database connectDatabase connect
Database connect
 
Jdbc
JdbcJdbc
Jdbc
 
Java JDBC
Java JDBCJava JDBC
Java JDBC
 
Jdbc Java Programming
Jdbc Java ProgrammingJdbc Java Programming
Jdbc Java Programming
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Prashanthi
PrashanthiPrashanthi
Prashanthi
 
JDBC programming
JDBC programmingJDBC programming
JDBC programming
 
JDBC in Servlets
JDBC in ServletsJDBC in Servlets
JDBC in Servlets
 
JDBC Connecticity.ppt
JDBC Connecticity.pptJDBC Connecticity.ppt
JDBC Connecticity.ppt
 
JDBC.ppt
JDBC.pptJDBC.ppt
JDBC.ppt
 
Jdbc oracle
Jdbc oracleJdbc oracle
Jdbc oracle
 
22jdbc
22jdbc22jdbc
22jdbc
 
JDBC Part - 2
JDBC Part - 2JDBC Part - 2
JDBC Part - 2
 
Session 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise JavaSession 24 - JDBC, Intro to Enterprise Java
Session 24 - JDBC, Intro to Enterprise Java
 
JDBC Tutorial
JDBC TutorialJDBC Tutorial
JDBC Tutorial
 
Jdbc
JdbcJdbc
Jdbc
 
Advance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-databaseAdvance Java Programming (CM5I)5.Interacting with-database
Advance Java Programming (CM5I)5.Interacting with-database
 

More from WebStackAcademy

Webstack Academy - Course Demo Webinar and Placement Journey
Webstack Academy - Course Demo Webinar and Placement JourneyWebstack Academy - Course Demo Webinar and Placement Journey
Webstack Academy - Course Demo Webinar and Placement JourneyWebStackAcademy
 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Scaling Web Service to Handle Millions of Requests per SecondWSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Scaling Web Service to Handle Millions of Requests per SecondWebStackAcademy
 
WSA: Course Demo Webinar - Full Stack Developer Course
WSA: Course Demo Webinar - Full Stack Developer CourseWSA: Course Demo Webinar - Full Stack Developer Course
WSA: Course Demo Webinar - Full Stack Developer CourseWebStackAcademy
 
Career Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and OpportunitiesCareer Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and OpportunitiesWebStackAcademy
 
Webstack Academy - Internship Kick Off
Webstack Academy - Internship Kick OffWebstack Academy - Internship Kick Off
Webstack Academy - Internship Kick OffWebStackAcademy
 
Building Your Online Portfolio
Building Your Online PortfolioBuilding Your Online Portfolio
Building Your Online PortfolioWebStackAcademy
 
Front-End Developer's Career Roadmap
Front-End Developer's Career RoadmapFront-End Developer's Career Roadmap
Front-End Developer's Career RoadmapWebStackAcademy
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationAngular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationWebStackAcademy
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesWebStackAcademy
 
Angular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase IntegrationAngular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase IntegrationWebStackAcademy
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - DirectivesWebStackAcademy
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event HandlingWebStackAcademy
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsAngular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsWebStackAcademy
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming WebStackAcademy
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - IntroductionWebStackAcademy
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and ArraysWebStackAcademy
 
JavaScript - Chapter 15 - Debugging Techniques
 JavaScript - Chapter 15 - Debugging Techniques JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 15 - Debugging TechniquesWebStackAcademy
 
JavaScript - Chapter 14 - Form Handling
 JavaScript - Chapter 14 - Form Handling   JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 14 - Form Handling WebStackAcademy
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)WebStackAcademy
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object ModelWebStackAcademy
 

More from WebStackAcademy (20)

Webstack Academy - Course Demo Webinar and Placement Journey
Webstack Academy - Course Demo Webinar and Placement JourneyWebstack Academy - Course Demo Webinar and Placement Journey
Webstack Academy - Course Demo Webinar and Placement Journey
 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Scaling Web Service to Handle Millions of Requests per SecondWSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Scaling Web Service to Handle Millions of Requests per Second
 
WSA: Course Demo Webinar - Full Stack Developer Course
WSA: Course Demo Webinar - Full Stack Developer CourseWSA: Course Demo Webinar - Full Stack Developer Course
WSA: Course Demo Webinar - Full Stack Developer Course
 
Career Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and OpportunitiesCareer Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and Opportunities
 
Webstack Academy - Internship Kick Off
Webstack Academy - Internship Kick OffWebstack Academy - Internship Kick Off
Webstack Academy - Internship Kick Off
 
Building Your Online Portfolio
Building Your Online PortfolioBuilding Your Online Portfolio
Building Your Online Portfolio
 
Front-End Developer's Career Roadmap
Front-End Developer's Career RoadmapFront-End Developer's Career Roadmap
Front-End Developer's Career Roadmap
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationAngular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and Authorization
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
 
Angular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase IntegrationAngular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase Integration
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsAngular - Chapter 3 - Components
Angular - Chapter 3 - Components
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
JavaScript - Chapter 15 - Debugging Techniques
 JavaScript - Chapter 15 - Debugging Techniques JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 15 - Debugging Techniques
 
JavaScript - Chapter 14 - Form Handling
 JavaScript - Chapter 14 - Form Handling   JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 14 - Form Handling
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 

Recently uploaded

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 

Recently uploaded (20)

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 

Core Java Programming Language (JSE) : Chapter XIII - JDBC

  • 3. 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.
  • 4. www.webstackacademy.com 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. ● That is why Sun Micro System has defined its own API (JDBC API) that uses JDBC driver written in Java language.
  • 5. www.webstackacademy.com 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 and javax.sql ● The latest version, JDBC 4.2, and is included in Java SE 8.
  • 6. www.webstackacademy.com 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.
  • 7. www.webstackacademy.com 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)
  • 8. www.webstackacademy.com 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.
  • 9. www.webstackacademy.com Type 2: Native-API Driver ● The Native API driver uses the client-side libraries of the database. ● It is not written entirely in Java.
  • 10. www.webstackacademy.com Type 3: Network Protocol Driver ● The Network Protocol driver uses middle ware (application server). ● It is fully written in Java.
  • 11. www.webstackacademy.com Type 4: Thin Driver ● The thin driver converts JDBC calls directly into the vendor-specific database protocol. ● That is why it is known as thin driver. ● It is fully written in Java language. ● Better performance than all other drivers. ● No software is required at client side or server side. ● Disadvantage: Drivers depends on the Database.
  • 12. www.webstackacademy.com 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
  • 13. www.webstackacademy.com Registering the Driver ● The forName() method of Class class is used to register the driver class. ● Class.forName("com.mysql.jdbc.Driver");
  • 14. www.webstackacademy.com 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","roo t","password");
  • 15. www.webstackacademy.com 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();
  • 16. www.webstackacademy.com 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)); }
  • 17. www.webstackacademy.com 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();
  • 18. www.webstackacademy.com 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/sonoo","root","root");
  • 19. www.webstackacademy.com 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
  • 20. www.webstackacademy.com 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","roo t","password"); ● DriverManager.registerDriver().
  • 21. www.webstackacademy.com 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.
  • 22. www.webstackacademy.com 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.
  • 23. www.webstackacademy.com 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.
  • 24. www.webstackacademy.com 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.
  • 25. www.webstackacademy.com 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();
  • 26. www.webstackacademy.com ResultSet interface ● The object of ResultSet maintains a cursor pointing to a particular row of data. ● Initially, cursor points to before the first row.
  • 27. www.webstackacademy.com 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.
  • 28. www.webstackacademy.com 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.
  • 29. www.webstackacademy.com 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();
  • 30. www.webstackacademy.com 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(?,?,?)";
  • 31. www.webstackacademy.com 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.
  • 32. www.webstackacademy.com 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.
  • 33. www.webstackacademy.com 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,"Ratan"); int i=stmt.executeUpdate(); System.out.println(i+" records inserted");
  • 34. www.webstackacademy.com Questions Write a program to implement CRUD in a table. Write a program to implement PreparedStatement?
  • 35. Web Stack Academy (P) Ltd #83, Farah Towers, 1st floor,MG Road, Bangalore – 560001 M: +91-80-4128 9576 T: +91-98862 69112 E: info@www.webstackacademy.com www.webstackacademy.com