DR - Menbere Adavanced OOP Database
DR - Menbere Adavanced OOP Database
1
Objective:
To introduce database systems,
and how to develop database
applications using Java.
2
Introduction
Database is an organized collection of data.
DBMS provides a mechanism to store, retrieve, organize
and modify data for many users.
Relational database – data is stored without consideration
of its physical structure.
SQL - a language used with relational database to perform
queries & to manipulate data.
E.g of RDBMSs: SQL Server, Oracle, Sybase, DB2,
MySQL, etc
Integrity Constraints - imposes a condition that all the
legal values in a table must satisfy.
3
Introduction (cont’d)
Three types of constraints: domain constraints,
primary key constraints, and foreign key
constraints.
Domain constraints specify the permissible values for an
attribute.
The foreign key constraints define the relationships among
relations.
4
Introduction (cont’d)
5
JDBC
The Java API for developing Java database applications is
called JDBC.
JDBC provides Java programmers with a uniform interface
for accessing and manipulating a wide range of relational
databases.
Using the JDBC API, applications written in the Java can
execute SQL statements, retrieve results, present data in a
user-friendly interface, and propagate changes back to the
database.
The JDBC API can also be used to interact with multiple
data sources in a distributed, heterogeneous environment.
6
JDBC (cont’d)
7
Developing Database Applications
The JDBC API is a Java application program interface to
generic SQL databases.
The JDBC API consists of classes and interfaces for:
Establishing connections with databases,
Sending SQL statements to databases,
Processing the results of the SQL statements, and
Obtaining database metadata.
8
Developing Database Applications (cont’d)
A JDBC application:
loads an appropriate driver using the Driver interface,
connects to the database using the Connection interface,
creates and executes SQL statements using the Statement
interface, and
processes the result using the ResultSet interface if the
statements return results.
9
Developing Database Applications (cont’d)
1. Loading drivers
An appropriate driver must be loaded using the
statement shown below before connecting to a
database.
Class.forName("JDBCDriverClass");
Database Driver Class
Access sun.jdbc.odbc.JdbcOdbcDriver
MySQL com.mysql.jdbc.Driver
Oracle oracle.jdbc.driver.OracleDriver
10
Developing Database Applications (cont’d)
2. Establishing connections.
To connect to a database, use the static method
getConnection(databaseURL) in the DriverManager
class, as follows:
Connection connection =
DriverManager.getConnection(databaseURL);
Example:
Connection connection = DriverManager.getConnection
("jdbc:odbc:ExampleMDBDataSource");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost/javabook
", "scott", "tiger");
11
Developing Database Applications (cont’d)
12
Developing Database Applications (cont’d)
3. Creating Statements
If a Connection object can be envisioned as a cable
linking your program to a database, an object of
Statement can be viewed as a cart that delivers SQL
statements for execution by the database and brings
the result back to the program
Once a Connection object is created, you can create
statements for executing SQL statements as follows:
Statement statement = connection.createStatement();
13
Developing Database Applications (cont’d)
4. Executing Statements
SQL DDL or update statement can be executed using
executeUpdate(String sql)
SQL query statement can be executed using
executeQuery(String sql).
The result of the query is returned in ResultSet.
For example, the following code executes the SQL
statement create table Temp (col1 char(5), col2 char(5)):
statement.executeUpdate("create table Temp (col1 char(5), col2
char(5))");
14
Developing Database Applications (cont’d)
15
Developing Database Applications (cont’d)
5. Processing ResultSet
The ResultSet maintains a table whose current row can be
retrieved.
The initial row position is null.
You can use the next method to move to the next row and
the various get methods to retrieve values from a current
row.
For example, the code given below displays all the results
from the preceding SQL query.
// Iterate through the result and print the student names
while (resultSet.next())
System.out.println(resultSet.getString(1) + " " +
resultSet.getString(2) + ". " + resultSet.getString(3));
16