Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
39 views18 pages

Chapter 5 - Database-1

Uploaded by

Ketema Deba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views18 pages

Chapter 5 - Database-1

Uploaded by

Ketema Deba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Chapter Seven

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.
 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

3
Introduction (cont’d)

A database system consists of data, database management


software, and application programs

4
Introduction (cont’d)

An application program may access multiple database systems

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)

Java programs access and manipulate databases through


JDBC drivers

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.

 Four key interfaces are needed to develop any database


application using Java: Driver, Connection, Statement, and
ResultSet.
 The JDBC API defines these interfaces.
 The JDBC driver vendors provide implementation for them.
Programmers use the interfaces.

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.

 Note that some statements, such as SQL data


definition statements and SQL data modification
statements, do not 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

JDBC Drivers

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)

Database URL Pattern


Access jdbc:odbc:dataSource
MySQL jdbc:mysql://hostname/dbname
Oracle jdbc:oracle:thin:@hostname:port#:oracleDBSID

JDBC URLs

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)

 The next code executes the SQL query select firstName,


mi, lastName from Student where lastName = 'Smith':
// Select the columns from the Student table
ResultSet resultSet = statement.executeQuery
("select firstName, mi, lastName from Student where lastName "
+ " = 'Smith'");

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
Examples
 MySqlConnect.java
 SelectFirstName.java
 SimpleSelect.java
 SelectWithConditions.java
 ReadFName.java
 UpdateTable.java
 CreateInsertTable.java
 DeleteFromTable.java

17
Examples(cont’d)
 SubQuery.java
 DBWithGUI.java
 DeleteIfExist.java
 AddToComboBox.java
 InsertWithGUI.java

18

You might also like