SlideShare a Scribd company logo
Java Database Connectivity(JDBC) UsingMySql
ADVANCE JAVA PROGRAMING
Subject Code (3360701)
Presented By- Dattani Dhyey-136250307505
JDBC Two TierArchitecture
• Java Application talks
directly to the database.
• Accomplished through the
JDBC driver which sends
commands directly to the
database.
• Results sent back directly to
the application
Application Space
Java Application
JDBC Driver
Database
SQL
Command
Result
Set
JDBC Three Tier Architecture
• JDBC driver sends
commands to a middle
tier, which in turn sends
commands to database.
• Results are sent back to
the middle tier, which
communicates them back
to the application
Application Space
Java Application
JDBC Driver
Database
SQL
Command
Result
Set
Application Server
(middle-tier)
Proprietary
Protocol
The JDBC API
 The JDBC API stands for Java Database Connectivity Application Programming Interface. It
allows an application written in java to communicate and interacts with database.
 It allows JAVA application to:
1) Create and open connection with database.
2) Specify and executes various SQL queries against database.
3) Retrieve records from database.
The JDBC API defines various classes and interfaces to communicate with database.
 The JDBC classes are defined inside java.sql package.
JDBC Components
Interface Purpose
Driver Is used to create a connection object using connect()
method.
Connection Is used to monitor and maintain database sessions.
createStatement() method is used create statement.
Statement Is used to execute SQL statements and retrieve records
from database.
ResultSet Is used to retrieve records that are returned by
executing SQL query.
1) The java.sql package :
 The java.sql package contains set of classes and interfaces that are used to
communicate with database.
 Following are most common interfaces of java.sql package.
JDBC Components
Class Purpose
DriverManager Is used to manage multiple drivers. And also used to
load and register the JDBC drivers and establish
connection with database. The getconnection() method
of DriverManager class is used to create connection
object.
SQLException This class handles any errors that occur in a database
application.
 Following are most common classes of java.sql package.
JDBC Components
2) JDBC Test Suite:
 The JDBC driver test suite helps you to determine that JDBC drivers will run your program. These
tests are not comprehensive or exhaustive, but they do exercise many of the important features in
the JDBC API.
3) JDBC-ODBC Bridge :
 The Java Software bridge provides JDBC access via ODBC drivers. Note that you need to load
ODBC binary code onto each client machine that uses this driver. As a result, the ODBC driver is
most appropriate on a corporate network where client installations are not a major problem, or for
application server code written in Java in a three-tier architecture.
JDBC-ODBC Bridge
 Advantages Of JDBC.
o Can read any database.
o Creates XML structure of data from database.
o No content conversion
o Query and stored procedure supported.
 Disadvantages Of JDBC.
o Not good for large project.
o It needs specific database queries.
o Multiple connections may have complexities
o Exception handling is a big issue with JDBC.
JDBC-ODBC Bridge
JDBC Drivers
 JDBC Driver is a software component that enables java application to interact with the
database.
 To help you understand what different drivers require, the following driver categorization
system id defined :-
o Type 1: JDBC-ODBC Bridge driver (Bridge).
o Type 2: Native-API/partly Java driver (Native).
o Type 3: All Java/Net-protocol driver (Middleware).
o Type 4: All Java/Native-protocol driver (Pure).
Type2: Native-API,PartlyJavaDriver
• Native-API driver converts
JDBC commands into
DBMS-specific native calls
• Directly interfaces with the
database
Application Space
Java Application
Type 2 JDBC Driver
Database
SQL
Command
Result
Set
Native Database
Library
Proprietary
Protocol
Type4: Native-Protocol,PureJavaDriver
 Pure Java drivers that
communicate directly with the
vendor’s database
 JDBC commands converted to
database engine’s native protocol
directly
 Advantage: no additional
translation or middleware layer
 Improves performance
Application Space
Java Application
Type 4 JDBC Driver
Database
SQL Command
Using Proprietary
Protocol
Result Set
Using Proprietary
Protocol
Step-1 : Import JAVA SQl statement.
o import.java.sql.*;
Creating Database
Step-2 : Load and Register JDBC driver.
o Syntax : Class.forName (“Driver Name”);
Step-3 : Establish Connection with Database.
o Syntax : Connection conn= DriverManager.getConnection (“URL”, “Username”, ”Password”);
Step-4 : Create Statement.
o Statement stmt = conn.createstatement();
Step-5 : Execute Query.
o ResultSet rs= stmt.executeQuery("SELECT * FROM STUDENT");
o stmt.executeUpdate("INSERT INTO STUDENT VALUES(7,'abc','Chennai')”);
Step-6 : Retrieve Results (applied for select query)
o while(rs.next())
{
int id = rs.getInt("enroll");
String name= rs.getString("name");
String city= rs.getString("city");
System.out.println(id+"tt");
System.out.println(name+"tt");
System.out.println(city+"tt");
}
Step-7 : Closing Connection and Statement.
o conn.close();
ostmt.close();
Continued…..
// Step-1 : Import java.sql package
import java.sql.*;
public class database
{
public static void main(String args[])
{
Connection conn= null;
Statement stmt= null;
try
{
//Step-2: Load and register the JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//Step-3 : Establish connection with Database.
System.out.println("Trying to connect with Database");
conn= DriverManager.getConnection("jdbc:mysql://localhost/","root","");
System.out.println("Connection Established Successfully");
//Step-4 : Create Statement.
System.out.println("Trying to create Database");
//Step-5 : Execute Query.
stmt=conn.createStatement();
String sql= "CREATE DATABASE jdemo";
stmt.executeUpdate(sql);
System.out.println("Database created successfully");
//Step-6: Close Connection.
conn.close();
stmt.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.sql.*;
public class dbpreparestmt
{
public static void main(String args[])
{
Connection conn = null;
Statement stmt = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Trying to connect with Database");
conn=DriverManager.getConnection("jdbc:mysql://localhost/jdemo","root","");
System.out.println("Connection Established Successfully");
System.out.println("Trying to insert data in table");
stmt = conn.createStatement();
PreparedStatement pst=conn.prepareStatement("INSERT INTO dhyey
VALUES(?,?,?)");
Insertion Using PrepareStatement
pst.setInt(1,7057);
pst.setString(2,"raj");
pst.setString(3,"gujrat");
pst.executeUpdate();
System.out.println("Data inserted successfully");
conn.close();
stmt.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

More Related Content

Similar to JDBC with MySQL.pdf

Unit 5.pdf
Unit 5.pdfUnit 5.pdf
Unit 5.pdf
saturo3011
 
chapter 5 java.pptx
chapter 5  java.pptxchapter 5  java.pptx
chapter 5 java.pptx
BekiTube
 
Jdbc
JdbcJdbc
JDBC java for learning java for learn.ppt
JDBC java for learning java for learn.pptJDBC java for learning java for learn.ppt
JDBC java for learning java for learn.ppt
kingkolju
 
jdbc
jdbcjdbc
jdbc
shreeuva
 
Basic Java Database Connectivity(JDBC)
Basic Java Database Connectivity(JDBC)Basic Java Database Connectivity(JDBC)
Basic Java Database Connectivity(JDBC)
suraj pandey
 
creating jdbc connection
creating jdbc connectioncreating jdbc connection
creating jdbc connection
Paneliya Prince
 
creating jdbc connection
creating jdbc connectioncreating jdbc connection
creating jdbc connection
Paneliya Prince
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
AliHusseini14
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
kamal kotecha
 
Jdbc complete
Jdbc completeJdbc complete
Jdbc complete
Sandeep Rawat
 
JDBC java database connectivity with dbms
JDBC java database connectivity with dbmsJDBC java database connectivity with dbms
JDBC java database connectivity with dbms
KhyalNayak
 
JDBC-Introduction
JDBC-IntroductionJDBC-Introduction
JDBC-Introduction
Mythili Shankar
 
java.pptx
java.pptxjava.pptx
java.pptx
bfgd1
 
Unit 5-jdbc2
Unit 5-jdbc2Unit 5-jdbc2
Unit 5-jdbc2
msafad
 
java 4 Part 1 computer science.pptx
java 4 Part 1 computer science.pptxjava 4 Part 1 computer science.pptx
java 4 Part 1 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Jdbc
JdbcJdbc
Jdbc new
Jdbc newJdbc new
Jdbc new
sumit kushwah
 

Similar to JDBC with MySQL.pdf (20)

Unit 5.pdf
Unit 5.pdfUnit 5.pdf
Unit 5.pdf
 
chapter 5 java.pptx
chapter 5  java.pptxchapter 5  java.pptx
chapter 5 java.pptx
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC java for learning java for learn.ppt
JDBC java for learning java for learn.pptJDBC java for learning java for learn.ppt
JDBC java for learning java for learn.ppt
 
jdbc
jdbcjdbc
jdbc
 
Basic Java Database Connectivity(JDBC)
Basic Java Database Connectivity(JDBC)Basic Java Database Connectivity(JDBC)
Basic Java Database Connectivity(JDBC)
 
creating jdbc connection
creating jdbc connectioncreating jdbc connection
creating jdbc connection
 
creating jdbc connection
creating jdbc connectioncreating jdbc connection
creating jdbc connection
 
Java jdbc
Java jdbcJava jdbc
Java jdbc
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
Jdbc complete
Jdbc completeJdbc complete
Jdbc complete
 
JDBC java database connectivity with dbms
JDBC java database connectivity with dbmsJDBC java database connectivity with dbms
JDBC java database connectivity with dbms
 
JDBC-Introduction
JDBC-IntroductionJDBC-Introduction
JDBC-Introduction
 
jdbc document
jdbc documentjdbc document
jdbc document
 
java.pptx
java.pptxjava.pptx
java.pptx
 
JDBC
JDBCJDBC
JDBC
 
Unit 5-jdbc2
Unit 5-jdbc2Unit 5-jdbc2
Unit 5-jdbc2
 
java 4 Part 1 computer science.pptx
java 4 Part 1 computer science.pptxjava 4 Part 1 computer science.pptx
java 4 Part 1 computer science.pptx
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc new
Jdbc newJdbc new
Jdbc new
 

More from Arumugam90

Notes for AR.ppt
Notes for AR.pptNotes for AR.ppt
Notes for AR.ppt
Arumugam90
 
Unity Tutorial_Highlighted Notes.pdf
Unity Tutorial_Highlighted Notes.pdfUnity Tutorial_Highlighted Notes.pdf
Unity Tutorial_Highlighted Notes.pdf
Arumugam90
 
AUGMENTED REALITY.pptx
AUGMENTED REALITY.pptxAUGMENTED REALITY.pptx
AUGMENTED REALITY.pptx
Arumugam90
 
Introductiontokaryotyping.pptx
Introductiontokaryotyping.pptxIntroductiontokaryotyping.pptx
Introductiontokaryotyping.pptx
Arumugam90
 
intro.ppt
intro.pptintro.ppt
intro.ppt
Arumugam90
 
Unit I_Computer Networks_2.ppt
Unit I_Computer Networks_2.pptUnit I_Computer Networks_2.ppt
Unit I_Computer Networks_2.ppt
Arumugam90
 
Unit_I_Computer Networks 4.pdf
Unit_I_Computer Networks 4.pdfUnit_I_Computer Networks 4.pdf
Unit_I_Computer Networks 4.pdf
Arumugam90
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
Arumugam90
 
Chapter16.ppt
Chapter16.pptChapter16.ppt
Chapter16.ppt
Arumugam90
 
Chapter15.ppt
Chapter15.pptChapter15.ppt
Chapter15.ppt
Arumugam90
 
HTTP, JSP, and AJAX.pdf
HTTP, JSP, and AJAX.pdfHTTP, JSP, and AJAX.pdf
HTTP, JSP, and AJAX.pdf
Arumugam90
 
JSP Components and Directives.pdf
JSP Components and Directives.pdfJSP Components and Directives.pdf
JSP Components and Directives.pdf
Arumugam90
 
JSP.pdf
JSP.pdfJSP.pdf
JSP.pdf
Arumugam90
 
Java Servlets.pdf
Java Servlets.pdfJava Servlets.pdf
Java Servlets.pdf
Arumugam90
 
JDBC with MySQL.pdf
JDBC with MySQL.pdfJDBC with MySQL.pdf
JDBC with MySQL.pdf
Arumugam90
 
JDBC.pdf
JDBC.pdfJDBC.pdf
JDBC.pdf
Arumugam90
 
DSJ_Unit III_Collection framework.pdf
DSJ_Unit III_Collection framework.pdfDSJ_Unit III_Collection framework.pdf
DSJ_Unit III_Collection framework.pdf
Arumugam90
 
DSJ_Unit III.pdf
DSJ_Unit III.pdfDSJ_Unit III.pdf
DSJ_Unit III.pdf
Arumugam90
 
DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdf
Arumugam90
 

More from Arumugam90 (20)

Notes for AR.ppt
Notes for AR.pptNotes for AR.ppt
Notes for AR.ppt
 
Unity Tutorial_Highlighted Notes.pdf
Unity Tutorial_Highlighted Notes.pdfUnity Tutorial_Highlighted Notes.pdf
Unity Tutorial_Highlighted Notes.pdf
 
AUGMENTED REALITY.pptx
AUGMENTED REALITY.pptxAUGMENTED REALITY.pptx
AUGMENTED REALITY.pptx
 
Introductiontokaryotyping.pptx
Introductiontokaryotyping.pptxIntroductiontokaryotyping.pptx
Introductiontokaryotyping.pptx
 
ML
MLML
ML
 
intro.ppt
intro.pptintro.ppt
intro.ppt
 
Unit I_Computer Networks_2.ppt
Unit I_Computer Networks_2.pptUnit I_Computer Networks_2.ppt
Unit I_Computer Networks_2.ppt
 
Unit_I_Computer Networks 4.pdf
Unit_I_Computer Networks 4.pdfUnit_I_Computer Networks 4.pdf
Unit_I_Computer Networks 4.pdf
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
 
Chapter16.ppt
Chapter16.pptChapter16.ppt
Chapter16.ppt
 
Chapter15.ppt
Chapter15.pptChapter15.ppt
Chapter15.ppt
 
HTTP, JSP, and AJAX.pdf
HTTP, JSP, and AJAX.pdfHTTP, JSP, and AJAX.pdf
HTTP, JSP, and AJAX.pdf
 
JSP Components and Directives.pdf
JSP Components and Directives.pdfJSP Components and Directives.pdf
JSP Components and Directives.pdf
 
JSP.pdf
JSP.pdfJSP.pdf
JSP.pdf
 
Java Servlets.pdf
Java Servlets.pdfJava Servlets.pdf
Java Servlets.pdf
 
JDBC with MySQL.pdf
JDBC with MySQL.pdfJDBC with MySQL.pdf
JDBC with MySQL.pdf
 
JDBC.pdf
JDBC.pdfJDBC.pdf
JDBC.pdf
 
DSJ_Unit III_Collection framework.pdf
DSJ_Unit III_Collection framework.pdfDSJ_Unit III_Collection framework.pdf
DSJ_Unit III_Collection framework.pdf
 
DSJ_Unit III.pdf
DSJ_Unit III.pdfDSJ_Unit III.pdf
DSJ_Unit III.pdf
 
DSJ_Unit I & II.pdf
DSJ_Unit I & II.pdfDSJ_Unit I & II.pdf
DSJ_Unit I & II.pdf
 

Recently uploaded

Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Subhajit Sahu
 
Business update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMIBusiness update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMI
AlejandraGmez176757
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
benishzehra469
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
ewymefz
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
NABLAS株式会社
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
ewymefz
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
James Polillo
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
axoqas
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar
 

Recently uploaded (20)

Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 
Business update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMIBusiness update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMI
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
 

JDBC with MySQL.pdf

  • 1. Java Database Connectivity(JDBC) UsingMySql ADVANCE JAVA PROGRAMING Subject Code (3360701) Presented By- Dattani Dhyey-136250307505
  • 2.
  • 3. JDBC Two TierArchitecture • Java Application talks directly to the database. • Accomplished through the JDBC driver which sends commands directly to the database. • Results sent back directly to the application Application Space Java Application JDBC Driver Database SQL Command Result Set
  • 4. JDBC Three Tier Architecture • JDBC driver sends commands to a middle tier, which in turn sends commands to database. • Results are sent back to the middle tier, which communicates them back to the application Application Space Java Application JDBC Driver Database SQL Command Result Set Application Server (middle-tier) Proprietary Protocol
  • 5. The JDBC API  The JDBC API stands for Java Database Connectivity Application Programming Interface. It allows an application written in java to communicate and interacts with database.  It allows JAVA application to: 1) Create and open connection with database. 2) Specify and executes various SQL queries against database. 3) Retrieve records from database. The JDBC API defines various classes and interfaces to communicate with database.  The JDBC classes are defined inside java.sql package.
  • 6. JDBC Components Interface Purpose Driver Is used to create a connection object using connect() method. Connection Is used to monitor and maintain database sessions. createStatement() method is used create statement. Statement Is used to execute SQL statements and retrieve records from database. ResultSet Is used to retrieve records that are returned by executing SQL query. 1) The java.sql package :  The java.sql package contains set of classes and interfaces that are used to communicate with database.  Following are most common interfaces of java.sql package.
  • 7. JDBC Components Class Purpose DriverManager Is used to manage multiple drivers. And also used to load and register the JDBC drivers and establish connection with database. The getconnection() method of DriverManager class is used to create connection object. SQLException This class handles any errors that occur in a database application.  Following are most common classes of java.sql package.
  • 8. JDBC Components 2) JDBC Test Suite:  The JDBC driver test suite helps you to determine that JDBC drivers will run your program. These tests are not comprehensive or exhaustive, but they do exercise many of the important features in the JDBC API. 3) JDBC-ODBC Bridge :  The Java Software bridge provides JDBC access via ODBC drivers. Note that you need to load ODBC binary code onto each client machine that uses this driver. As a result, the ODBC driver is most appropriate on a corporate network where client installations are not a major problem, or for application server code written in Java in a three-tier architecture.
  • 9. JDBC-ODBC Bridge  Advantages Of JDBC. o Can read any database. o Creates XML structure of data from database. o No content conversion o Query and stored procedure supported.  Disadvantages Of JDBC. o Not good for large project. o It needs specific database queries. o Multiple connections may have complexities o Exception handling is a big issue with JDBC.
  • 11. JDBC Drivers  JDBC Driver is a software component that enables java application to interact with the database.  To help you understand what different drivers require, the following driver categorization system id defined :- o Type 1: JDBC-ODBC Bridge driver (Bridge). o Type 2: Native-API/partly Java driver (Native). o Type 3: All Java/Net-protocol driver (Middleware). o Type 4: All Java/Native-protocol driver (Pure).
  • 12.
  • 13.
  • 14.
  • 15. Type2: Native-API,PartlyJavaDriver • Native-API driver converts JDBC commands into DBMS-specific native calls • Directly interfaces with the database Application Space Java Application Type 2 JDBC Driver Database SQL Command Result Set Native Database Library Proprietary Protocol
  • 16.
  • 17.
  • 18. Type4: Native-Protocol,PureJavaDriver  Pure Java drivers that communicate directly with the vendor’s database  JDBC commands converted to database engine’s native protocol directly  Advantage: no additional translation or middleware layer  Improves performance Application Space Java Application Type 4 JDBC Driver Database SQL Command Using Proprietary Protocol Result Set Using Proprietary Protocol
  • 19. Step-1 : Import JAVA SQl statement. o import.java.sql.*; Creating Database Step-2 : Load and Register JDBC driver. o Syntax : Class.forName (“Driver Name”); Step-3 : Establish Connection with Database. o Syntax : Connection conn= DriverManager.getConnection (“URL”, “Username”, ”Password”); Step-4 : Create Statement. o Statement stmt = conn.createstatement();
  • 20. Step-5 : Execute Query. o ResultSet rs= stmt.executeQuery("SELECT * FROM STUDENT"); o stmt.executeUpdate("INSERT INTO STUDENT VALUES(7,'abc','Chennai')”); Step-6 : Retrieve Results (applied for select query) o while(rs.next()) { int id = rs.getInt("enroll"); String name= rs.getString("name"); String city= rs.getString("city"); System.out.println(id+"tt"); System.out.println(name+"tt"); System.out.println(city+"tt"); } Step-7 : Closing Connection and Statement. o conn.close(); ostmt.close(); Continued…..
  • 21. // Step-1 : Import java.sql package import java.sql.*; public class database { public static void main(String args[]) { Connection conn= null; Statement stmt= null; try { //Step-2: Load and register the JDBC driver Class.forName("com.mysql.jdbc.Driver"); //Step-3 : Establish connection with Database. System.out.println("Trying to connect with Database"); conn= DriverManager.getConnection("jdbc:mysql://localhost/","root",""); System.out.println("Connection Established Successfully"); //Step-4 : Create Statement. System.out.println("Trying to create Database");
  • 22. //Step-5 : Execute Query. stmt=conn.createStatement(); String sql= "CREATE DATABASE jdemo"; stmt.executeUpdate(sql); System.out.println("Database created successfully"); //Step-6: Close Connection. conn.close(); stmt.close(); } catch(SQLException se) { se.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } } }
  • 23. import java.sql.*; public class dbpreparestmt { public static void main(String args[]) { Connection conn = null; Statement stmt = null; try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("Trying to connect with Database"); conn=DriverManager.getConnection("jdbc:mysql://localhost/jdemo","root",""); System.out.println("Connection Established Successfully"); System.out.println("Trying to insert data in table"); stmt = conn.createStatement(); PreparedStatement pst=conn.prepareStatement("INSERT INTO dhyey VALUES(?,?,?)"); Insertion Using PrepareStatement