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

What's hot

Cisco CCNA ITN (Introduction to Networks) - Certification
Cisco CCNA ITN (Introduction to Networks) - CertificationCisco CCNA ITN (Introduction to Networks) - Certification
Cisco CCNA ITN (Introduction to Networks) - Certification
Mohamed Haathim Sharfraz
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
kumar gaurav
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
Hitesh-Java
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
Vikas Jagtap
 
Servlets
ServletsServlets
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
Sony India Software Center
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
Sunil OS
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
Dzmitry Naskou
 
Java Networking
Java NetworkingJava Networking
Java Networking
Sunil OS
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
Rasheed Waraich
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
CS8392 OOP
CS8392 OOPCS8392 OOP
Introduce yourself to java 17
Introduce yourself to java 17Introduce yourself to java 17
Introduce yourself to java 17
ankitbhandari32
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
Prognoz Technologies Pvt. Ltd.
 

What's hot (20)

Cisco CCNA ITN (Introduction to Networks) - Certification
Cisco CCNA ITN (Introduction to Networks) - CertificationCisco CCNA ITN (Introduction to Networks) - Certification
Cisco CCNA ITN (Introduction to Networks) - Certification
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
 
Java
JavaJava
Java
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
JNDI
JNDIJNDI
JNDI
 
Ajax
AjaxAjax
Ajax
 
Servlets
ServletsServlets
Servlets
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 
Presentation1
Presentation1Presentation1
Presentation1
 
Spring framework core
Spring framework coreSpring framework core
Spring framework core
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
 
CS8392 OOP
CS8392 OOPCS8392 OOP
CS8392 OOP
 
Introduce yourself to java 17
Introduce yourself to java 17Introduce yourself to java 17
Introduce yourself to java 17
 
Introduction to package in java
Introduction to package in javaIntroduction to package in java
Introduction to package in java
 

Similar to Chapter6 database connectivity

jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
DrMeenakshiS
 
Advance Java Practical file
Advance Java Practical fileAdvance Java Practical file
Advance Java Practical file
varun arora
 
Jdbc new
Jdbc newJdbc new
Jdbc new
sumit kushwah
 
4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt
NaveenKumar648465
 
Java Data Base Connectivity concepts.pptx
Java Data Base Connectivity concepts.pptxJava Data Base Connectivity concepts.pptx
Java Data Base Connectivity concepts.pptx
mukeshprasanth909
 
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 EntityFrameworkAkhil Mittal
 
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
vishal choudhary
 
Android sql examples
Android sql examplesAndroid sql examples
Android sql examples
Aravindharamanan S
 
Lecture 1. java database connectivity
Lecture 1. java database connectivityLecture 1. java database connectivity
Lecture 1. java database connectivity
Waheedullah Suliman Khail
 
Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)Java DataBase Connectivity API (JDBC API)
Java DataBase Connectivity API (JDBC API)
Luzan Baral
 
IRJET- Review on Java Database Connectivity
IRJET- Review on Java Database ConnectivityIRJET- Review on Java Database Connectivity
IRJET- Review on Java Database Connectivity
IRJET Journal
 
Final Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptFinal Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.ppt
TabassumMaktum
 
unit 3.docx
unit 3.docxunit 3.docx
unit 3.docx
Sadhana Sreekanth
 
LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLAkhil Mittal
 
Jdbc
JdbcJdbc
JDBC
JDBCJDBC
jsp MySQL database connectivity
jsp MySQL database connectivityjsp MySQL database connectivity
jsp MySQL database connectivity
baabtra.com - No. 1 supplier of quality freshers
 

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

STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
kimdan468
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 

Recently uploaded (20)

STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBCSTRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
STRAND 3 HYGIENIC PRACTICES.pptx GRADE 7 CBC
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 

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