SlideShare a Scribd company logo
5
Most read
21
Most read
23
Most read
Java Database Connectivity(JDBC) UsingMySql
ADVANCE JAVA PROGRAMING
Subject Code (3360701)
Presented By- Dattani Dhyey-136250307505
JDBC with MySQL.pdf
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).
JDBC with MySQL.pdf
JDBC with MySQL.pdf
JDBC with MySQL.pdf
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
JDBC with MySQL.pdf
JDBC with MySQL.pdf
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

PPTX
Java web application development
PPTX
Control Structure in JavaScript (1).pptx
PPS
Jdbc architecture and driver types ppt
PDF
Struts2
PPT
Mvc architecture
PPT
Java Servlets
PPTX
Introduction to Spring Framework
PPTX
Java Server Pages(jsp)
Java web application development
Control Structure in JavaScript (1).pptx
Jdbc architecture and driver types ppt
Struts2
Mvc architecture
Java Servlets
Introduction to Spring Framework
Java Server Pages(jsp)

What's hot (20)

PPTX
Installation guide for mysql in windows
PPTX
ASP.NET Page Life Cycle
PDF
Introduction to Web Services
PPTX
Types of Drivers in JDBC
PPTX
operating system
PDF
Introduction to Loops in Java | For, While, Do While, Infinite Loops | Edureka
PPT
Servlet life cycle
PPTX
Notes Unit2.pptx
PDF
Spring MVC Framework
PPTX
enterprise java bean
PPTX
Introduction to ASP.NET
PDF
Spring mvc
PPTX
PPTX
Jdbc ppt
PPT
Introduction to .NET Framework
PPSX
Entity beans in java
DOCX
Servlet
Installation guide for mysql in windows
ASP.NET Page Life Cycle
Introduction to Web Services
Types of Drivers in JDBC
operating system
Introduction to Loops in Java | For, While, Do While, Infinite Loops | Edureka
Servlet life cycle
Notes Unit2.pptx
Spring MVC Framework
enterprise java bean
Introduction to ASP.NET
Spring mvc
Jdbc ppt
Introduction to .NET Framework
Entity beans in java
Servlet
Ad

Similar to JDBC with MySQL.pdf (20)

PPT
Jdbc ppt
PPTX
Jdbc introduction
PPTX
jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
PPT
PDF
JDBC : Java Database Connectivity
PPT
4-INTERDUCATION TO JDBC-2019.ppt
PPTX
Java Database Connectivity (JDBC)
PPTX
jdbc-130913021409-phpapp01000988www.pptx
PDF
Unit 5.pdf
PPTX
chapter 5 java.pptx
PPT
JDBC java for learning java for learn.ppt
PDF
jdbc
PPT
Basic Java Database Connectivity(JDBC)
PDF
unit8_jdbc.pdf mysql and java jdbc connection
PPTX
creating jdbc connection
PPTX
creating jdbc connection
PPTX
Java jdbc
PPT
Jdbc complete
PPT
JDBC java database connectivity with dbms
Jdbc ppt
Jdbc introduction
jdbc Java Database Connectivity ujjwal matoliya jdbc.pptx
JDBC : Java Database Connectivity
4-INTERDUCATION TO JDBC-2019.ppt
Java Database Connectivity (JDBC)
jdbc-130913021409-phpapp01000988www.pptx
Unit 5.pdf
chapter 5 java.pptx
JDBC java for learning java for learn.ppt
jdbc
Basic Java Database Connectivity(JDBC)
unit8_jdbc.pdf mysql and java jdbc connection
creating jdbc connection
creating jdbc connection
Java jdbc
Jdbc complete
JDBC java database connectivity with dbms
Ad

More from Arumugam90 (20)

PPTX
Type Script notes for Data Structures using java
PPTX
Data Structures using java notes for MCA
PPTX
Progressive Web Application Development for MCA
PPT
Notes for AR.ppt
PDF
Unity Tutorial_Highlighted Notes.pdf
PPTX
AUGMENTED REALITY.pptx
PPTX
Introductiontokaryotyping.pptx
PDF
PPT
intro.ppt
PPT
Unit I_Computer Networks_2.ppt
PDF
Unit_I_Computer Networks 4.pdf
PPT
CS3114_09212011.ppt
PPT
Chapter16.ppt
PPT
Chapter15.ppt
PDF
HTTP, JSP, and AJAX.pdf
PDF
JSP Components and Directives.pdf
PDF
JSP.pdf
PDF
Java Servlets.pdf
PDF
JDBC with MySQL.pdf
PDF
JDBC.pdf
Type Script notes for Data Structures using java
Data Structures using java notes for MCA
Progressive Web Application Development for MCA
Notes for AR.ppt
Unity Tutorial_Highlighted Notes.pdf
AUGMENTED REALITY.pptx
Introductiontokaryotyping.pptx
intro.ppt
Unit I_Computer Networks_2.ppt
Unit_I_Computer Networks 4.pdf
CS3114_09212011.ppt
Chapter16.ppt
Chapter15.ppt
HTTP, JSP, and AJAX.pdf
JSP Components and Directives.pdf
JSP.pdf
Java Servlets.pdf
JDBC with MySQL.pdf
JDBC.pdf

Recently uploaded (20)

PPTX
Steganography Project Steganography Project .pptx
PPTX
Topic 5 Presentation 5 Lesson 5 Corporate Fin
PDF
Data Engineering Interview Questions & Answers Cloud Data Stacks (AWS, Azure,...
PDF
Systems Analysis and Design, 12th Edition by Scott Tilley Test Bank.pdf
PPTX
Phase1_final PPTuwhefoegfohwfoiehfoegg.pptx
PPTX
modul_python (1).pptx for professional and student
PPTX
sac 451hinhgsgshssjsjsjheegdggeegegdggddgeg.pptx
PDF
OneRead_20250728_1808.pdfhdhddhshahwhwwjjaaja
DOCX
Factor Analysis Word Document Presentation
PPTX
SET 1 Compulsory MNH machine learning intro
PPT
Predictive modeling basics in data cleaning process
PPTX
Business_Capability_Map_Collection__pptx
PDF
Votre score augmente si vous choisissez une catégorie et que vous rédigez une...
PPTX
STERILIZATION AND DISINFECTION-1.ppthhhbx
PPTX
chrmotography.pptx food anaylysis techni
PPTX
SAP 2 completion done . PRESENTATION.pptx
PPTX
Lesson-01intheselfoflifeofthekennyrogersoftheunderstandoftheunderstanded
PPTX
A Complete Guide to Streamlining Business Processes
PPTX
Managing Community Partner Relationships
PPTX
IMPACT OF LANDSLIDE.....................
Steganography Project Steganography Project .pptx
Topic 5 Presentation 5 Lesson 5 Corporate Fin
Data Engineering Interview Questions & Answers Cloud Data Stacks (AWS, Azure,...
Systems Analysis and Design, 12th Edition by Scott Tilley Test Bank.pdf
Phase1_final PPTuwhefoegfohwfoiehfoegg.pptx
modul_python (1).pptx for professional and student
sac 451hinhgsgshssjsjsjheegdggeegegdggddgeg.pptx
OneRead_20250728_1808.pdfhdhddhshahwhwwjjaaja
Factor Analysis Word Document Presentation
SET 1 Compulsory MNH machine learning intro
Predictive modeling basics in data cleaning process
Business_Capability_Map_Collection__pptx
Votre score augmente si vous choisissez une catégorie et que vous rédigez une...
STERILIZATION AND DISINFECTION-1.ppthhhbx
chrmotography.pptx food anaylysis techni
SAP 2 completion done . PRESENTATION.pptx
Lesson-01intheselfoflifeofthekennyrogersoftheunderstandoftheunderstanded
A Complete Guide to Streamlining Business Processes
Managing Community Partner Relationships
IMPACT OF LANDSLIDE.....................

JDBC with MySQL.pdf

  • 1. Java Database Connectivity(JDBC) UsingMySql ADVANCE JAVA PROGRAMING Subject Code (3360701) Presented By- Dattani Dhyey-136250307505
  • 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).
  • 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
  • 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