SlideShare a Scribd company logo
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

PPTX
android sqlite
PPTX
Core java
PPT
Backbone.js
PPTX
React workshop presentation
PDF
JavaFX Overview
PDF
Angular directives and pipes
PPTX
Introduction to Apache Spark
android sqlite
Core java
Backbone.js
React workshop presentation
JavaFX Overview
Angular directives and pipes
Introduction to Apache Spark

What's hot (20)

PDF
Understanding and Improving Code Generation
PPTX
Servlets
PDF
Enterprise java unit-3_chapter-1-jsp
PDF
Cypress Automation Testing Tutorial (Part 1).pdf
PPTX
Spring boot
PPTX
Intro to React
PDF
Workshop 21: React Router
PDF
Spring boot introduction
PDF
React JS - Introduction
PPT
PPTX
React js for beginners
PPTX
Introduction to Spring Boot
PPTX
[Final] ReactJS presentation
PDF
React js
PPTX
Building Data Pipelines with Spark and StreamSets
PPT
General introduction to intellij idea
PPTX
Core java complete ppt(note)
PPTX
Enterprise java unit-2_chapter-1
PPTX
Introduction to MongoDB and CRUD operations
PDF
Full-Stack Development with Spring Boot and VueJS
Understanding and Improving Code Generation
Servlets
Enterprise java unit-3_chapter-1-jsp
Cypress Automation Testing Tutorial (Part 1).pdf
Spring boot
Intro to React
Workshop 21: React Router
Spring boot introduction
React JS - Introduction
React js for beginners
Introduction to Spring Boot
[Final] ReactJS presentation
React js
Building Data Pipelines with Spark and StreamSets
General introduction to intellij idea
Core java complete ppt(note)
Enterprise java unit-2_chapter-1
Introduction to MongoDB and CRUD operations
Full-Stack Development with Spring Boot and VueJS
Ad

Similar to Chapter6 database connectivity (20)

PPT
jdbc_presentation.ppt
PPTX
JDBCshwdbyuqwgdihbjwbdigwydwiuhsouhso NEW1.pptx
PDF
Advance Java Practical file
PPTX
Jdbc new
PPTX
PPTX
jdbcppt.pptx , jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt
PPTX
jdbcppt.pptx , jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt
PPT
4-INTERDUCATION TO JDBC-2019.ppt
PDF
1586279370_Bsc(P)-VI-InternetTechnologies-3.pdf
PPTX
Java Data Base Connectivity concepts.pptx
PPTX
Lecture 10: Android SQLite database.pptx
DOCX
Learning MVC Part 3 Creating MVC Application with EntityFramework
PPTX
Create an android app for database creation using.pptx
PPTX
JDBC PPT(4).pptx java Database Connectivity
PPTX
21CS642 Module 5 JDBC PPT.pptx VI SEM CSE Students
DOCX
Android sql examples
PPTX
Lecture 1. java database connectivity
PPTX
Java DataBase Connectivity API (JDBC API)
jdbc_presentation.ppt
JDBCshwdbyuqwgdihbjwbdigwydwiuhsouhso NEW1.pptx
Advance Java Practical file
Jdbc new
jdbcppt.pptx , jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt
jdbcppt.pptx , jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt jdbc ppt
4-INTERDUCATION TO JDBC-2019.ppt
1586279370_Bsc(P)-VI-InternetTechnologies-3.pdf
Java Data Base Connectivity concepts.pptx
Lecture 10: Android SQLite database.pptx
Learning MVC Part 3 Creating MVC Application with EntityFramework
Create an android app for database creation using.pptx
JDBC PPT(4).pptx java Database Connectivity
21CS642 Module 5 JDBC PPT.pptx VI SEM CSE Students
Android sql examples
Lecture 1. java database connectivity
Java DataBase Connectivity API (JDBC API)
Ad

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
O7-L3 Supply Chain Operations - ICLT Program
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Insiders guide to clinical Medicine.pdf
PPH.pptx obstetrics and gynecology in nursing
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Anesthesia in Laparoscopic Surgery in India
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
VCE English Exam - Section C Student Revision Booklet
Week 4 Term 3 Study Techniques revisited.pptx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Microbial disease of the cardiovascular and lymphatic systems
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Pharma ospi slides which help in ospi learning
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
O7-L3 Supply Chain Operations - ICLT Program

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( ) ;