You are on page 1of 27

Object-Oriented Programming

(OOP)

Java Database Programming


19th September, 2019

Dr. Ramesh Kumar


Assistant Professor
Electronic Engineering Department, DUET
Karachi
Database
• Database systems are everywhere and play an
important role in society and in commerce
• Your personal information is stored in a database by
the government (NADRA)
• If you shop online, your purchase information is
stored in a database by the company
• If you attend a university, your academic information
is stored in a database by the university
• Database systems not only store data, they also
provide means of accessing, updating,
manipulating, and analyzing data

2
Database
• A database system consists of a database, the
software that stores and manages data in the
database, and the application programs that present
data and enable the user to interact with the database
system

3
Database
• A database is a repository of data that form information.
— Database Management Systems: MySQL, Oracle, IBM’s DB2
and Informix etc.
• Database management systems are designed for use by
professional programmers
• Application programs are built on top of the DBMS for
customers to access and update the database.
• Application programs can be viewed as the interfaces
between the database system and its users
• Application programs may be stand-alone GUI
applications or Web applications

4
Database

5
Relational Database
• Most of today’s database systems are relational
database systems
— Structure: Defines the representation of the data
— Integrity: Imposes constraints on the data
— Language: Provides the means for accessing and
manipulating data
• The relational model is built around a simple and
natural structure.
• A relation is actually a table that consists of non-
duplicate rows
• Tables are easy to understand and use

6
Relational Database
• A row of a table represents a record
• A column of a table represents the value of a single
attribute of the record
• In relational database theory, a row is called a tuple
and a column is called an attribute

7
Relational Database
• Unique values
• No duplicate rows
• Non-empty columns

8
Relational Database
• Integrity Constraints
— Domain: specify the permissible values for an attribute
— Primary-Key: Used to identify tuples uniquely in a relation
— Foreign-Key: Define the relationships among relations
• The standard data type specifies a broad range of
values
• Additional constraints can be specified to narrow
the ranges & specify whether an attribute can be
null create table Course (courseId char(5),
subjectId char(4) not null,
courseNumber integer, title
varchar(50) not null, numOfCredits
integer, constraint greaterThanOne
check (numOfCredits >= 1)); ); 9
Supper Key
• A superkey is an attribute or a set of attributes that
uniquely identify the relation
• That is, no two tuples have the same values on the
superkey
• By definition, a relation consists of a set of
• distinct tuples
• The set of all attributes in the relation forms a
superkey.

10
Structured Query Language (SQL)
• Standard language for querying and manipulating
data
• Two Major Parts
— Data Definition Language
— Data Manipulation Language
• Data Definition Language (DDL)
— Create/alter/delete tables and their attributes
• Data Manipulation Language (DML)
— Query one or more tables
— Insert/delete/modify rows in tables

11
Database Table
Table name Attribute names

Product
PID PName Price Category Manufacturer

1101 Gizmo $19.99 Gadgets GizmoWorks

1102 Powergizmo $29.99 Gadgets GizmoWorks

1103 SingleTouch $149.99 Photography Canon

1104 MultiTouch $203.99 Household Hitachi

Tuples or rows 12
Database Table
• The schema of a table is the table name and its
attributes:
• Product(PID, PName, Price, Category, Manufacturer)

• A key is an attribute whose values are unique;

• Underline a key below


• Product(PID, PName, Price, Category, Manufacturer)

13
Creating a Table
CREATE TABLE Course (courseId char(5),
courseNumber integer, courseTitle varchar(50) not
null, numOfCredits integer, primary key (courseId) ) );

CREATE TABLE Student (ssn char(9), firstName varchar(25),


middleName varchar(1), lastName varchar(25), birthDate
date, street varchar(25), phone char(11), zipCode char(5),
deptId char(4), primary key (ssn) );

CREATE TABLE Enrollment ( ssn char(9), courseId char(5),


dateRegistered date, grade char(1), primary key (ssn,
courseId), foreign key (ssn) references Student(ssn),
foreign key (courseId) references Course(courseId)
14
Insert, Update, and Delete
INSERT INTO Course (courseId, subjectId,
courseNumber, title, numOfCredits)
values ('11113', 'CSCI', '3720', 'Database Systems', 3);

UPDATE Course
set numOfCredits = 4
where title = 'Database Systems'

DELETE from Course


where title = 'Database Systems';

15
SQL Query
• Basic form

SELECT
SELECT <attributes>
<attributes>
FROM
FROM <one
<one or
or more
more relations>
relations>
WHERE
WHERE <conditions>
<conditions>

16
Simple SQL Query
Product
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

SELECT
SELECT **
FROM
FROM Product
Product
WHERE
WHERE category=‘Gadgets’
category=‘Gadgets’

PName Price Category Manufacturer


“selection” Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
17
Simple SQL Query
Product
PName Price Category Manufacturer
Gizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks
SingleTouch $149.99 Photography Canon
MultiTouch $203.99 Household Hitachi

SELECT
SELECT PName,
PName,Price,
Price,Manufacturer
Manufacturer
FROM
FROM Product
Product
WHERE
WHERE Price
Price>>100
100
PName Price Manufacturer
“selection” and SingleTouch $149.99 Canon
“projection” MultiTouch $203.99 Hitachi

18
Joining two tables

SELECT DISTINCT lastName, firstName, courseId


FROM Student, Enrollment
WHERE Student.ssn = Enrollment.ssn and
lastName = 'Smith' and firstName = 'Jacob';

19
Java Database Connectivity (JDBC)
• JDBC is the Java API for accessing relational database
• A set of Java interfaces and classes used to write
Java programs for accessing and manipulating
relational databases
• Using the JDBC API, applications written in the Java
programming language can
—Execute SQL statements
—Retrieve results
—Present data in a user-friendly interface
—Propagate changes back to the database

20
Java Database Connectivity (JDBC)
• JDBC driver: Interface to
facilitate communications
between JDBC & database
• JDBC drivers are database
specific
• Provided by the database
vendors
• MySQL JDBC driver
• Oracle JDBC driver
• JDBC-ODBC bridge driver

21
Developing Database Applications
Using JDBC
• Four key interfaces are needed to develop any database
application using Java:
—Driver
—Connection
—Statement
—ResultSet
• These interfaces define a framework for generic SQL
database access
• The JDBC API defines these interfaces
• JDBC driver vendors provide the implementation for the
interfaces
• Programmers use these interfaces

22
Developing Database Applications
Using JDBC
• A JDBC application loads an appropriate driver using
Driver interface
• Connects to database using Connection interface
• Creates & executes SQL statements using
Statement
• Processes the result using the ResultSet interface if
the statements return results

23
Database accessing Steps
• Loading drivers ( Class.forName("JDBCDriverClass") )
• Add mysql-connector-java-5.1.26.jar and ojdbc6.jar
in the classpath
• set classpath=%classpath%;c:\book\lib\mysql-
connector-java-5.1.26.jar;
• set classpath=%classpath%; c:\book\lib\ojdbc6.jar;

24
Database accessing Steps
• Establishing connections
— Access: Connection connection =
DriverManager.getConnection("jdbc:odbc:ExampleMDBD
ataSource");
— MySQL: Connection connection =
DriverManager.getConnection("jdbc:mysql://localhos
t/javabook", "scott", "tiger")
— Oracle: Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@lia
ng.armstrong.edu:1521:orcl","scott", "tiger");

25
Database accessing Steps
• Creating statements
— Statement statement =
connection.createStatement();
• Executing statements
— statement.executeUpdate ("create table Temp
(col1 char(5), col2 char(5))");
— ResultSet resultSet =statement.executeQuery
("select firstName, mi, lastName from Student
where lastName “ + " = 'Smith'");
• Processing Results
— System.out.println(resultSet.getString(1) + " "
+ resultSet.getString(2) + " " +
resultSet.getString(3));

26
Simple JDBC Program

27

You might also like