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.