SlideShare a Scribd company logo
1 of 8
Download to read offline
Database Connectivity
Front-End
Every IT application provides some sort of form using which users enter the
data. This form is called the Front End Interface of the application.
Back-End Database
IT application usually stores a lot of data in the form of a database which is
not visible to the user. This database is used by the application to give
suitable responses to the user. This database is called Back-End Database.
Database Connectivity (Front-End + Back-End)
The two components are essential to establish a database connection that
allows the front end to communicate with the back end.
1. The JDBC ( Java Database Connectivity ) API – Allows us to access
MySQL database and execute MySQL statements (like Insert,
Update,Delete etc.) in java code.
2. The JDBC Driver for MySQL - Software component enabling a java
application to interact with a MySQL database.
API - An application programming interface (API) is an interface
implemented by a software program which enables it to interact with other
software. It facilitates interaction between different software programs
similar to the way the user interface facilitates interaction between users
and computers.
Adding [MySQL JDBC Driver] Library in NetBeans
To add the MySQL JDBC Driver Library follows the given steps.
Step 1: Right click on the Project name and select the Properties option.
Step 2: In the Properties dialog box,
1. Choose the Libraries option from the Categories pane.
2. Click on the Add Library button.
3. From the Add Library dialog box choose the MySQL JDBC Driver.
4. Click on Add Library Button
Step 1: Right click on the Project name and select the Properties option.
Step 2: In the Properties dialog box,
1. Choose the Libraries option from the Categories pane
2. Click on the Add Library button as shown in Figure
3. From the Add Library dialog box choose the MySQL JDBC Driver
4. Click on Add Library Button
5. The driver is now added to the compile time libraries
6. Now MySql JDBC Driver is inserted in our project
C:Program Files (x86)NetBeans 7.0idemodulesextmysql-
connector-java-5.1.13-bin.jar
CLASSESS USED FOR DATABASE CONNECTIVITY
Following classes are essential for setting up the connection with the
database and retrieve data from the database.
1. DriverManager Class – This class defines objects which can connect
Java applications to a JDBC driver. DriverManager class manages the
JDBC drivers that are installed on the system.
2. Connection Class – Manages the communication between a java
application and a specific database (e.g. MySQL database).
3. Statement Class - To execute SQL statements, we need to instantiate
(create) a Statement object using the connection object. A Statement
object is used to send and execute SQL statements to a database.
4. ResultSet Class – Contains predefined methods to access, analyze, and
convert data values returned by an executed SQL select statement.
Steps for creating Database Connectivity Applications :
Step 1 : Import the Packages Required for Database Programming
This step consists of two sub-steps :
(i) Import the library packages
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.Statement ;
import java.sql.ResultSet ;
or
import java.sql.* ;
(ii) Add the MySQL JDBC connector.
Step 2 - Register the JDBC Driver – Register the jdbc driver with the
DriverManager to open a communication channel with the database from
within the Java application.
Class named Class offers a method called forName( ) registers the driver
with the DriverManager.
Syntax :
Class.forName ( “driver name”) ;
Example:
Class.forName ( “java.sql.Driver”) ;
Or
Class.forName ( “com.mysql.jdbc.Driver” );
Step 3 - Open a Connection – getConnection() method of DriverManager
class is used to establish a connection to a database.
It uses –
 Username.
 Password.
 JDBC URL to establish a connection to the database
and returns a connection object.
DriverManager.getConnection ( <DB_URL>, <userid>, <password> ) ;
DriverManager.getConnection ( “jdbc:mysql://localhost:3306/cbse”, “root”,
“kvnmh” ) ;
The getConnection ( ) method returns a Connection object , thus we must
store its return value in a Connection object.
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost/cbse","root","kvnmh"
);
The Connection object allows us to establish a physical connection to the
database.
Step 4: Execute a Query – First create an object of type Statement for
building and submitting an SQL statement to the database using
createStatement method of Connection type object.
Statement stmt = con.createStatement ( );
Next we need to execute the SQL statement using excuteQuery( ) method.
The executeQuery method returns a resultset ( an object of ResultSet type)
that contains the resultant dataset of the executed query, thus we must
store the returned value of executeQuery( ) into a ResultSet object.
ResultSet rs =stmt.executeQuery ( “SELECT ID, FIRSTNAME, LASTNAME,
AGE FROM EMPLOYEE ; ”) ;
OR
String sql = “SELECT ID, FIRSTNAME, LASTNAME, AGE FROM EMPLOYEE” ;
ResultSet rs = stmt.executeQuery (sql) ;
Step 5 : Extract Data from Result Set – This step is required in case we
are fetching data from the database.
The ResultSet object provides several methods for obtaining column data
for a row.
int id = re.getInt(1) ; // retrieves the 1st
column (int type)
String firstname = rs.getString(2) ; /* retrieves the 2nd
column
(String type) */
OR
int id = rs.getInt(“id”) ; // column name (int type)
String fname = rs.getString (“firstname”) ; /* column name ( String
type) */
Step 6 : Clean up the Environment : Close all database resources using
close() method.
rs.close( ) ;
stmt.close( ) ;
con.colse( ) ;

More Related Content

What's hot

Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections frameworkRiccardo Cardin
 
Java servlets
Java servletsJava servlets
Java servletslopjuan
 
Inheritance in c++
Inheritance in c++ Inheritance in c++
Inheritance in c++ sandeep54552
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 
Java collections concept
Java collections conceptJava collections concept
Java collections conceptkumar gaurav
 
Enterprise java unit-1_chapter-1
Enterprise java unit-1_chapter-1Enterprise java unit-1_chapter-1
Enterprise java unit-1_chapter-1sandeep54552
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPTPooja Jaiswal
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)Maher Abdo
 
OWASP A4 XML External Entities (XXE)
OWASP A4 XML External Entities (XXE)OWASP A4 XML External Entities (XXE)
OWASP A4 XML External Entities (XXE)Michael Furman
 
Dapper - Rise of the MicroORM
Dapper - Rise of the MicroORMDapper - Rise of the MicroORM
Dapper - Rise of the MicroORMSquareHire
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivityTanmoy Barman
 

What's hot (20)

JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
JDBC ppt
JDBC pptJDBC ppt
JDBC ppt
 
Java Beans
Java BeansJava Beans
Java Beans
 
Java servlets
Java servletsJava servlets
Java servlets
 
Inheritance in c++
Inheritance in c++ Inheritance in c++
Inheritance in c++
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Session bean
Session beanSession bean
Session bean
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Enterprise java unit-1_chapter-1
Enterprise java unit-1_chapter-1Enterprise java unit-1_chapter-1
Enterprise java unit-1_chapter-1
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Jdbc (database in java)
Jdbc (database in java)Jdbc (database in java)
Jdbc (database in java)
 
Ado.net
Ado.netAdo.net
Ado.net
 
OWASP A4 XML External Entities (XXE)
OWASP A4 XML External Entities (XXE)OWASP A4 XML External Entities (XXE)
OWASP A4 XML External Entities (XXE)
 
Dapper - Rise of the MicroORM
Dapper - Rise of the MicroORMDapper - Rise of the MicroORM
Dapper - Rise of the MicroORM
 
Java swing
Java swingJava swing
Java swing
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Java Collections
Java  Collections Java  Collections
Java Collections
 

Similar to Chapter6 database connectivity

Similar to Chapter6 database connectivity (20)

jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
 
Jsp project module
Jsp project moduleJsp project module
Jsp project module
 
Advance Java Practical file
Advance Java Practical fileAdvance Java Practical file
Advance Java Practical file
 
Jdbc new
Jdbc newJdbc new
Jdbc new
 
Jdbc
JdbcJdbc
Jdbc
 
4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt
 
Java Data Base Connectivity concepts.pptx
Java Data Base Connectivity concepts.pptxJava Data Base Connectivity concepts.pptx
Java Data Base Connectivity concepts.pptx
 
Learning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFrameworkLearning MVC Part 3 Creating MVC Application with EntityFramework
Learning MVC Part 3 Creating MVC Application with EntityFramework
 
Create an android app for database creation using.pptx
Create an android app for database creation using.pptxCreate an android app for database creation using.pptx
Create an android app for database creation using.pptx
 
Jdbc
JdbcJdbc
Jdbc
 
Android sql examples
Android sql examplesAndroid sql examples
Android sql examples
 
Lecture 1. java database connectivity
Lecture 1. java database connectivityLecture 1. java database connectivity
Lecture 1. java database connectivity
 
Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)
 
IRJET- Review on Java Database Connectivity
IRJET- Review on Java Database ConnectivityIRJET- Review on Java Database Connectivity
IRJET- Review on Java Database Connectivity
 
Final Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptFinal Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.ppt
 
unit 3.docx
unit 3.docxunit 3.docx
unit 3.docx
 
LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQL
 
Jdbc
JdbcJdbc
Jdbc
 
JDBC
JDBCJDBC
JDBC
 
jsp MySQL database connectivity
jsp MySQL database connectivityjsp MySQL database connectivity
jsp MySQL database connectivity
 

Recently uploaded

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 

Chapter6 database connectivity

  • 1. Database Connectivity Front-End Every IT application provides some sort of form using which users enter the data. This form is called the Front End Interface of the application. Back-End Database IT application usually stores a lot of data in the form of a database which is not visible to the user. This database is used by the application to give suitable responses to the user. This database is called Back-End Database. Database Connectivity (Front-End + Back-End) The two components are essential to establish a database connection that allows the front end to communicate with the back end. 1. The JDBC ( Java Database Connectivity ) API – Allows us to access MySQL database and execute MySQL statements (like Insert, Update,Delete etc.) in java code. 2. The JDBC Driver for MySQL - Software component enabling a java application to interact with a MySQL database.
  • 2. API - An application programming interface (API) is an interface implemented by a software program which enables it to interact with other software. It facilitates interaction between different software programs similar to the way the user interface facilitates interaction between users and computers. Adding [MySQL JDBC Driver] Library in NetBeans To add the MySQL JDBC Driver Library follows the given steps. Step 1: Right click on the Project name and select the Properties option. Step 2: In the Properties dialog box, 1. Choose the Libraries option from the Categories pane. 2. Click on the Add Library button. 3. From the Add Library dialog box choose the MySQL JDBC Driver. 4. Click on Add Library Button Step 1: Right click on the Project name and select the Properties option.
  • 3. Step 2: In the Properties dialog box, 1. Choose the Libraries option from the Categories pane 2. Click on the Add Library button as shown in Figure
  • 4. 3. From the Add Library dialog box choose the MySQL JDBC Driver 4. Click on Add Library Button
  • 5. 5. The driver is now added to the compile time libraries 6. Now MySql JDBC Driver is inserted in our project C:Program Files (x86)NetBeans 7.0idemodulesextmysql- connector-java-5.1.13-bin.jar CLASSESS USED FOR DATABASE CONNECTIVITY Following classes are essential for setting up the connection with the database and retrieve data from the database.
  • 6. 1. DriverManager Class – This class defines objects which can connect Java applications to a JDBC driver. DriverManager class manages the JDBC drivers that are installed on the system. 2. Connection Class – Manages the communication between a java application and a specific database (e.g. MySQL database). 3. Statement Class - To execute SQL statements, we need to instantiate (create) a Statement object using the connection object. A Statement object is used to send and execute SQL statements to a database. 4. ResultSet Class – Contains predefined methods to access, analyze, and convert data values returned by an executed SQL select statement. Steps for creating Database Connectivity Applications : Step 1 : Import the Packages Required for Database Programming This step consists of two sub-steps : (i) Import the library packages import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.Statement ; import java.sql.ResultSet ; or import java.sql.* ; (ii) Add the MySQL JDBC connector. Step 2 - Register the JDBC Driver – Register the jdbc driver with the DriverManager to open a communication channel with the database from within the Java application. Class named Class offers a method called forName( ) registers the driver with the DriverManager. Syntax : Class.forName ( “driver name”) ; Example:
  • 7. Class.forName ( “java.sql.Driver”) ; Or Class.forName ( “com.mysql.jdbc.Driver” ); Step 3 - Open a Connection – getConnection() method of DriverManager class is used to establish a connection to a database. It uses –  Username.  Password.  JDBC URL to establish a connection to the database and returns a connection object. DriverManager.getConnection ( <DB_URL>, <userid>, <password> ) ; DriverManager.getConnection ( “jdbc:mysql://localhost:3306/cbse”, “root”, “kvnmh” ) ; The getConnection ( ) method returns a Connection object , thus we must store its return value in a Connection object. Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/cbse","root","kvnmh" ); The Connection object allows us to establish a physical connection to the database. Step 4: Execute a Query – First create an object of type Statement for building and submitting an SQL statement to the database using createStatement method of Connection type object. Statement stmt = con.createStatement ( ); Next we need to execute the SQL statement using excuteQuery( ) method. The executeQuery method returns a resultset ( an object of ResultSet type)
  • 8. that contains the resultant dataset of the executed query, thus we must store the returned value of executeQuery( ) into a ResultSet object. ResultSet rs =stmt.executeQuery ( “SELECT ID, FIRSTNAME, LASTNAME, AGE FROM EMPLOYEE ; ”) ; OR String sql = “SELECT ID, FIRSTNAME, LASTNAME, AGE FROM EMPLOYEE” ; ResultSet rs = stmt.executeQuery (sql) ; Step 5 : Extract Data from Result Set – This step is required in case we are fetching data from the database. The ResultSet object provides several methods for obtaining column data for a row. int id = re.getInt(1) ; // retrieves the 1st column (int type) String firstname = rs.getString(2) ; /* retrieves the 2nd column (String type) */ OR int id = rs.getInt(“id”) ; // column name (int type) String fname = rs.getString (“firstname”) ; /* column name ( String type) */ Step 6 : Clean up the Environment : Close all database resources using close() method. rs.close( ) ; stmt.close( ) ; con.colse( ) ;