SlideShare a Scribd company logo
1 of 29
Chapter 7
Java Database Connectivity (JDBC)
1 Compiled By Mikiyas D.
Outline
 Database Systems – an Introduction
 Structured Query Language
 Installing and setting up JDBC
 Basic JDBC Programming concepts
 Populating a database
 Executing Queries
2 Compiled By Mikiyas D.
Introduction
3
 What is Database ?
 The database is a collection of inter-related data which is used to retrieve,
insert and delete the data efficiently.
 It is also used to organize the data in the form of a table, schema, views,
and reports, etc.
 What is Database Management System ?
 Database management system is a software which is used to manage the
database. For example: MySQL, Oracle, etc are a very popular commercial
database which is used in different applications.
 DBMS provides an interface to perform various operations like database
creation, storing data in it, updating data, creating a table in the database
and a lot more.
Compiled By Mikiyas D.
4
 SQL is a short-form of the structured query language
 This database language is mainly designed for maintaining the data in
relational database management systems.
 It is a special tool used by data professionals for handling structured data
(data which is stored in the form of tables).
 Types of SQL Commands
 Data Definition Language (DDL)
 Data Manipulation Language (DML)
 Data Query Language (DQL)
Compiled By Mikiyas D.
Structured Query Language (SQL)
5
 Data Definition Language (DDL)
 DDL changes the structure of the table like creating a table, deleting a
table, altering a table, etc.
 Here are some commands that come under DDL:
 CREATE It is used to create a new table in the database.
 Syntax:
 CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
 Example:
 CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(100), D
OB DATE);
Compiled By Mikiyas D.
Cont’d….
6
 DROP: It is used to delete both the structure and record stored in the
table.
 Syntax:
DROP TABLE table_name;
 Example:
DROP TABLE EMPLOYEE;
 ALTER: It is used to alter the structure of the database. This change could
be either to modify the characteristics of an existing attribute or probably
to add a new attribute.
 Syntax: To add a new column in the table
ALTER TABLE table_name ADD column_name COLUMN-definition;
Compiled By Mikiyas D.
Cont’d….
7
 Syntax: To modify existing column in the table
ALTER TABLE table_name MODIFY(column_definitions....);
 Example:
ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));
ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));
Compiled By Mikiyas D.
Cont’d….
8
 Data Manipulation Language (DML)
 DML commands are used to modify the database. It is responsible for all
form of changes in the database.
 Here are some commands that come under DML:
 INSERT: The INSERT statement is a SQL query. It is used to insert data
into the row of a table.
 Syntax:
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES (value1, value2, value3, .... valueN); or
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
 Example:
INSERT INTO Student (Name, Id) VALUES (“Han", “R1234/23");
Compiled By Mikiyas D.
Cont’d….
9
 UPDATE: This command is used to update or modify the value of a column in
the table.
 Syntax:
UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE CONDITION]
 Example:
UPDATE student SET Name = ’Hana’ WHERE Id = “R1234/23";
 DELETE: It is used to remove one or more row from a table.
 Syntax:
DELETE FROM table_name [WHERE condition];
 Example:
DELETE FROM Student WHERE Id=" R1234/23";
Compiled By Mikiyas D.
Cont’d….
10
 Data Query Language (DQL)
 DQL is used to fetch the data from the database.
 It uses only one command: select
 SELECT: This is the same as the projection operation of relational algebra.
It is used to select the attribute based on the condition described by
WHERE clause.
 Syntax:
SELECT expressions FROM TABLE_NAME WHERE conditions;
 Example:
SELECT emp_name FROM employee WHERE age > 20;
Compiled By Mikiyas D.
Cont’d….
Introduction to JDBC
11
 JDBC is a Java API to connect and execute the query with the
database.
 JDBC API uses JDBC drivers to connect with the database.
 We can use JDBC API to access tabular data stored in any relational
database.
 By the help of JDBC API, we can save, update, delete and fetch data
from the database.
 The java.sql package contains classes and interfaces for JDBC API.
Compiled By Mikiyas D.
JDBC Architecture
Compiled By Mikiyas D.
JDBC Architecture (cont.)
Application JDBC Driver
 Java code calls JDBC library
 JDBC loads a driver
 Driver talks to a particular database
 An application can work with several databases by using
all corresponding drivers
Compiled By Mikiyas D.
JDBC Steps…..
14
 There are 5 steps to connect any java application with
the database using JDBC. These steps are as follows:
 Register the Driver class
 Create connection
 Create statement
 Execute queries
 Close connection
Compiled By Mikiyas D.
JDBC Steps…..
15
 Step 1: Register the driver class
 The forName() method of Class is used to register the driver class.
 This method is used to dynamically load the driver class.
 Example
 Class.forName(“com.mysql.jdbc.Driver ”);
 Step 2: Create the connection object
 The getConnection() method of DriverManager class is used to establish
connection with the database.
Compiled By Mikiyas D.
Cont’d…….
16
 Syntax for Creating Connection
1. public static Connection getConnection(String url)throws SQLExcept
ion
2. public static Connection getConnection(String url,String name,String
password) throws SQLException
 Example:
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/miki ",“root","password");
Compiled By Mikiyas D.
Cont’d…….
17
 Step 3: Create the Statement object
 The createStatement() method of Connection interface is used to create
statement.
 The object of statement is responsible to execute queries with the
database.
 Syntax of createStatement() method
public Statement createStatement() throws SQLException
 Example
 Statement stmt=con.createStatement();
 or
Compiled By Mikiyas D.
Cont’d…….
18
 Creating prepared Statement
 Syntax:
 PreparedStatement pstmt=connObj.prepareStatement(sql);
 Example:
 PreparedStatement pStmt = conn.prepareStatement("UPDATE
Sells SET price = ? WHERE bar = ? AND beer = ?");
pStmt.setDouble(1,58);
pStmt.setString(2,”Hilten”);
pStmt.setString(3,”Heinken”);
 Why use PreparedStatement?
 Improves performance: The performance of the application will be faster if
you use PreparedStatement interface because query is compiled only once
Compiled By Mikiyas D.
Cont’d…….
19
 Step 4: Execute the Statement
 executeQuery(): executes a simple SELECT query and returns a single ResultSet object
 executeUpdate(): executes the SQL INSERT, UPDATE, and DELETE statement, and
 Example: pStmt.executeUpdate(); //executes the sql in the previous slide
 executeQuery method returns the object of ResultSet that can be used to
get all the records of a table.
 Syntax of executeQuery() method
public ResultSet executeQuery(String sql) throws SQLException
 Example
Compiled By Mikiyas D.
Cont’d…….
20
 Step 5: Close the connection object
 By closing connection object Statement and ResultSet will be closed
automatically.
 The close() method of Connection interface is used to close the connection.
 Syntax of close() method
public void close() throws SQLException
 Example
con.close();
Compiled By Mikiyas D.
Connecting to MYSQL Database Example
21
 First download MYSQL JDBC connector/Driver on the link:
https://dev.mysql.com/downloads/connector/j/5.1.html
 Then on NetBeans create New Project, goto the project you Created, Right
click on the Library, Click on Add JAR/Folder, then locate the path where
you place your MYSQL Connector, and add it to the library.
Compiled By Mikiyas D.
Cont’d………..
22 Compiled By Mikiyas D.
Cont’d…….
23
 Create Database and Tables on MYSQL, then
Compiled By Mikiyas D.
Cont’d…….
24 Compiled By Mikiyas D.
Cont’d…….
25 Compiled By Mikiyas D.
Cont’d…….
26 Compiled By Mikiyas D.
Cont’d…….
27 Compiled By Mikiyas D.
Cont’d…….
28 Compiled By Mikiyas D.
29 Compiled By Mikiyas D.

More Related Content

Similar to Chapter Seven- JDBC.pptx (20)

7. SQL.pptx
7. SQL.pptx7. SQL.pptx
7. SQL.pptx
 
Java Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedJava Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet Advanced
 
MAD UNIT 5 FINAL.pptx
MAD UNIT 5 FINAL.pptxMAD UNIT 5 FINAL.pptx
MAD UNIT 5 FINAL.pptx
 
Jdbc
JdbcJdbc
Jdbc
 
ASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NETASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NET
 
03-JDBC.pptx
03-JDBC.pptx03-JDBC.pptx
03-JDBC.pptx
 
Ado.net
Ado.netAdo.net
Ado.net
 
RMySQL Tutorial For Beginners
RMySQL Tutorial For BeginnersRMySQL Tutorial For Beginners
RMySQL Tutorial For Beginners
 
Jdbc example program with access and MySql
Jdbc example program with access and MySqlJdbc example program with access and MySql
Jdbc example program with access and MySql
 
Lecture17
Lecture17Lecture17
Lecture17
 
Spring framework part 2
Spring framework part 2Spring framework part 2
Spring framework part 2
 
Lecture 1. java database connectivity
Lecture 1. java database connectivityLecture 1. java database connectivity
Lecture 1. java database connectivity
 
Ado.Net
Ado.NetAdo.Net
Ado.Net
 
Spring framework DAO
Spring framework  DAOSpring framework  DAO
Spring framework DAO
 
Sqlapi0.1
Sqlapi0.1Sqlapi0.1
Sqlapi0.1
 
JDBC Tutorial
JDBC TutorialJDBC Tutorial
JDBC Tutorial
 
JDBC (2).ppt
JDBC (2).pptJDBC (2).ppt
JDBC (2).ppt
 
MySQL Presentation
MySQL PresentationMySQL Presentation
MySQL Presentation
 
JDBC
JDBCJDBC
JDBC
 
Android sq lite-chapter 22
Android sq lite-chapter 22Android sq lite-chapter 22
Android sq lite-chapter 22
 

Recently uploaded

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
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
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
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
 
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
 
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
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
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
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 

Recently uploaded (20)

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
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🔝
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
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 🔝✔️✔️
 
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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
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
 
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
 
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
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
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
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

Chapter Seven- JDBC.pptx

  • 1. Chapter 7 Java Database Connectivity (JDBC) 1 Compiled By Mikiyas D.
  • 2. Outline  Database Systems – an Introduction  Structured Query Language  Installing and setting up JDBC  Basic JDBC Programming concepts  Populating a database  Executing Queries 2 Compiled By Mikiyas D.
  • 3. Introduction 3  What is Database ?  The database is a collection of inter-related data which is used to retrieve, insert and delete the data efficiently.  It is also used to organize the data in the form of a table, schema, views, and reports, etc.  What is Database Management System ?  Database management system is a software which is used to manage the database. For example: MySQL, Oracle, etc are a very popular commercial database which is used in different applications.  DBMS provides an interface to perform various operations like database creation, storing data in it, updating data, creating a table in the database and a lot more. Compiled By Mikiyas D.
  • 4. 4  SQL is a short-form of the structured query language  This database language is mainly designed for maintaining the data in relational database management systems.  It is a special tool used by data professionals for handling structured data (data which is stored in the form of tables).  Types of SQL Commands  Data Definition Language (DDL)  Data Manipulation Language (DML)  Data Query Language (DQL) Compiled By Mikiyas D. Structured Query Language (SQL)
  • 5. 5  Data Definition Language (DDL)  DDL changes the structure of the table like creating a table, deleting a table, altering a table, etc.  Here are some commands that come under DDL:  CREATE It is used to create a new table in the database.  Syntax:  CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);  Example:  CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(100), D OB DATE); Compiled By Mikiyas D. Cont’d….
  • 6. 6  DROP: It is used to delete both the structure and record stored in the table.  Syntax: DROP TABLE table_name;  Example: DROP TABLE EMPLOYEE;  ALTER: It is used to alter the structure of the database. This change could be either to modify the characteristics of an existing attribute or probably to add a new attribute.  Syntax: To add a new column in the table ALTER TABLE table_name ADD column_name COLUMN-definition; Compiled By Mikiyas D. Cont’d….
  • 7. 7  Syntax: To modify existing column in the table ALTER TABLE table_name MODIFY(column_definitions....);  Example: ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20)); ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20)); Compiled By Mikiyas D. Cont’d….
  • 8. 8  Data Manipulation Language (DML)  DML commands are used to modify the database. It is responsible for all form of changes in the database.  Here are some commands that come under DML:  INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of a table.  Syntax: INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES (value1, value2, value3, .... valueN); or INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);  Example: INSERT INTO Student (Name, Id) VALUES (“Han", “R1234/23"); Compiled By Mikiyas D. Cont’d….
  • 9. 9  UPDATE: This command is used to update or modify the value of a column in the table.  Syntax: UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE CONDITION]  Example: UPDATE student SET Name = ’Hana’ WHERE Id = “R1234/23";  DELETE: It is used to remove one or more row from a table.  Syntax: DELETE FROM table_name [WHERE condition];  Example: DELETE FROM Student WHERE Id=" R1234/23"; Compiled By Mikiyas D. Cont’d….
  • 10. 10  Data Query Language (DQL)  DQL is used to fetch the data from the database.  It uses only one command: select  SELECT: This is the same as the projection operation of relational algebra. It is used to select the attribute based on the condition described by WHERE clause.  Syntax: SELECT expressions FROM TABLE_NAME WHERE conditions;  Example: SELECT emp_name FROM employee WHERE age > 20; Compiled By Mikiyas D. Cont’d….
  • 11. Introduction to JDBC 11  JDBC is a Java API to connect and execute the query with the database.  JDBC API uses JDBC drivers to connect with the database.  We can use JDBC API to access tabular data stored in any relational database.  By the help of JDBC API, we can save, update, delete and fetch data from the database.  The java.sql package contains classes and interfaces for JDBC API. Compiled By Mikiyas D.
  • 13. JDBC Architecture (cont.) Application JDBC Driver  Java code calls JDBC library  JDBC loads a driver  Driver talks to a particular database  An application can work with several databases by using all corresponding drivers Compiled By Mikiyas D.
  • 14. JDBC Steps….. 14  There are 5 steps to connect any java application with the database using JDBC. These steps are as follows:  Register the Driver class  Create connection  Create statement  Execute queries  Close connection Compiled By Mikiyas D.
  • 15. JDBC Steps….. 15  Step 1: Register the driver class  The forName() method of Class is used to register the driver class.  This method is used to dynamically load the driver class.  Example  Class.forName(“com.mysql.jdbc.Driver ”);  Step 2: Create the connection object  The getConnection() method of DriverManager class is used to establish connection with the database. Compiled By Mikiyas D.
  • 16. Cont’d……. 16  Syntax for Creating Connection 1. public static Connection getConnection(String url)throws SQLExcept ion 2. public static Connection getConnection(String url,String name,String password) throws SQLException  Example: Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/miki ",“root","password"); Compiled By Mikiyas D.
  • 17. Cont’d……. 17  Step 3: Create the Statement object  The createStatement() method of Connection interface is used to create statement.  The object of statement is responsible to execute queries with the database.  Syntax of createStatement() method public Statement createStatement() throws SQLException  Example  Statement stmt=con.createStatement();  or Compiled By Mikiyas D.
  • 18. Cont’d……. 18  Creating prepared Statement  Syntax:  PreparedStatement pstmt=connObj.prepareStatement(sql);  Example:  PreparedStatement pStmt = conn.prepareStatement("UPDATE Sells SET price = ? WHERE bar = ? AND beer = ?"); pStmt.setDouble(1,58); pStmt.setString(2,”Hilten”); pStmt.setString(3,”Heinken”);  Why use PreparedStatement?  Improves performance: The performance of the application will be faster if you use PreparedStatement interface because query is compiled only once Compiled By Mikiyas D.
  • 19. Cont’d……. 19  Step 4: Execute the Statement  executeQuery(): executes a simple SELECT query and returns a single ResultSet object  executeUpdate(): executes the SQL INSERT, UPDATE, and DELETE statement, and  Example: pStmt.executeUpdate(); //executes the sql in the previous slide  executeQuery method returns the object of ResultSet that can be used to get all the records of a table.  Syntax of executeQuery() method public ResultSet executeQuery(String sql) throws SQLException  Example Compiled By Mikiyas D.
  • 20. Cont’d……. 20  Step 5: Close the connection object  By closing connection object Statement and ResultSet will be closed automatically.  The close() method of Connection interface is used to close the connection.  Syntax of close() method public void close() throws SQLException  Example con.close(); Compiled By Mikiyas D.
  • 21. Connecting to MYSQL Database Example 21  First download MYSQL JDBC connector/Driver on the link: https://dev.mysql.com/downloads/connector/j/5.1.html  Then on NetBeans create New Project, goto the project you Created, Right click on the Library, Click on Add JAR/Folder, then locate the path where you place your MYSQL Connector, and add it to the library. Compiled By Mikiyas D.
  • 23. Cont’d……. 23  Create Database and Tables on MYSQL, then Compiled By Mikiyas D.
  • 29. 29 Compiled By Mikiyas D.