Chapter 4 - Database
Chapter 4 - Database
Code: SWEG2033
Chapter four
Java Database Connectivity
1
Objective:
• To introduce database systems, and how to develop
database applications using Java.
2
Introduction
Database is an organized collection of data(file) having rule to access it.
Relational database is a set of tables containing data fitted into
predefined categories. Each table (which is sometimes called a relation)
contains one or more data categories in columns.
To using relational database programs, a standard language called
Structured Query Language (SQL) was developed.
SQL is the industry-standard approach to accessing relational databases.
SQL(Structured Query Language): a language used with relational
database to perform queries & to manipulate data. SQL is a standard for
database access that has been adopted by virtually all database vendors.
In database programming, a request for records in a database is called a
query.
3
Introduction
DBMS provides a mechanism to store, retrieve, organize and modify data for
many users on database.
E.g., of RDBMSs: SQL Server, Oracle, Sybase, DB2, MySQL, etc.
Integrity Constraints : Integrity constraints provide a way of ensuring that
changes made to the database by authorized users do not result in a loss of data
consistency.
4
Introduction
5
JDBC
With JDBC, you can use the same methods and classes in Java programs
to read and write records and perform other kinds of database access.
A class called a driver acts as a bridge to the database source. There are
drivers for each of the popular databases.
6
JDBC
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.
7
JDBC
8
JDBC
9
JDBC
10
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/making connections with databases,
Crating and Sending SQL statements to databases,
Executing that SQL query in the database
Processing/Viewing the results of the SQL statements, and
Obtaining database metadata.
N.B: These JDBC classes all are part of the java.sql package.
Four key interfaces are needed to develop any database application using Java:
Driver, Connection, Statement, and ResultSet.
The JDBC driver vendors provide implementation for them. Programmers use
the interfaces.
11
Developing Database Applications
A JDBC application:
Loads an appropriate driver using the Driver interface,
Creates and executes SQL statements using the Statement interface, and
Processes the result using the ResultSet interface if the statements return
results.
13
Developing Database Applications
Access sun.jdbc.odbc.JdbcOdbcDriver
MySQL com.mysql.jdbc.Driver
Oracle oracle.jdbc.driver.OracleDriver
14
Developing Database Applications
15
Developing Database Applications
16
Developing Database Applications
17
Developing Database Applications
4. Creating Statements
18
Developing Database Applications
5. 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))");
19
Developing Database Applications
The next code executes the SQL query select firstName, mi,
lastName from Student where lastName = 'Smith':
// Select the columns from the Student table
+ " = 'Smith'");
20
Developing Database Applications
6. 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));
21
Developing Database Applications
22