SlideShare una empresa de Scribd logo
1 de 8
Descargar para leer sin conexión
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( ) ;

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Web Technology Lab files with practical
Web Technology Lab  files with practicalWeb Technology Lab  files with practical
Web Technology Lab files with practical
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
C# loops
C# loopsC# loops
C# loops
 
Method Overloading in Java
Method Overloading in JavaMethod Overloading in Java
Method Overloading in Java
 
Database development life cycle
Database development life cycleDatabase development life cycle
Database development life cycle
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Lecture 01 introduction to database
Lecture 01 introduction to databaseLecture 01 introduction to database
Lecture 01 introduction to database
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
 
Java Array String
Java Array StringJava Array String
Java Array String
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Io streams
Io streamsIo streams
Io streams
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Trigger
TriggerTrigger
Trigger
 
Java - Generic programming
Java - Generic programmingJava - Generic programming
Java - Generic programming
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Database Security
Database SecurityDatabase Security
Database Security
 

Similar a Chapter6 database connectivity

Similar a 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
 

Último

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 

Último (20)

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 

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