Está en la página 1de 14


Database Application Development

SSK 3408
Chapter 5
Client-Server Application
(2-Tier Architecture)

Interfacing with the Database

Data stored in a database in a proprietary format.

Application faces the same problem as Notepad or any other application trying to
access data in an unknown format.

A software interface is needed between your web application and the database
allowing the application and the database to talk to each other.

Three common interfaces let applications communicate with databases.

Open Database Connectivity, or ODBC;

OLE DB (object linking & embedding database); and

Java Database Connectivity, or JDBC.

The same table open with Notepad:

Understanding JSP Connection

A JSP application must connect to a database

through a JDBC driver.
The driver acts as an interpreter that lets a JSP
application communicate with a database.
Specify certain parameter values to connect
through your JDBC driver. For the parameter
values specific to your driver, see the driver
vendors documentation or consult your system
You can also use an ODBC driver (and so a
Windows DSN) if you have a JDBC-ODBC

Understanding JSP Connection



A standard API (Application Program Interface) for

accessing relational databases from a Java program.
This interface makes it easy to access a database
because it provides an abstract layer that hides the lowlevel details, such as managing sockets.
The basic steps to get program up and running are:

Load the driver

Define the connection URL

Establish the connection

Create a statement

Execute a query and retrieve the results, or make changes to the


Disconnect from the database

classes12.jar &


To Start up Orion Server

JDBC Detail Process

1. Load the driver


2. Define the Connection URL

String hostname =;
int port = 1521;
String sid = orcl;
String oracleURL = jdbc:oracle:thin:@+hostname+:+port+:+sid;

JDBC Detail Process ..cont.

3. Establish the Connection

String username = salmi;
String password = secret;
Connection conn = DriverManager.getConnection(oracleURL, username, password);

4. Create a Statement
Statement stmt = conn.createStatement();

5. Execute a Query
String query = Select empid, fname, salary, commission from employee;
Resultset rs = stmt.executeQuery(query);

JDBC Detail Process ..cont.

6. Process the Result

while ( {
out.println (Empid: +rs.getString(1));
out.println (First Name: +rs.getString(2));
out.println (Salary: +rs.getString(3));
out.println (Commission: +rs.getString(4));

7. Close the Connection


Basic JDBC Example

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
try {
String hostname =";
int port = 1521;
String sid = "orcl";
String oracleURL = "jdbc:oracle:thin:@"+hostname+":"+port+":"+sid;
String username = XXXX";
String password = XXXX";
Connection conn = DriverManager.getConnection(oracleURL, username, password);
Statement stmt = conn.createStatement();
String query = "Select empid, fname, salary, commission from employee";
ResultSet rs = stmt.executeQuery(query);

Basic JDBC Example cont.

int ctr = 1;
out.println("<H1>MY FIRST JSP</H1>");
out.println("<table border = 1>");
while ( {
out.println("<tr><td>" +
ctr + "</td><td> " +
rs.getString(1) + "</td><td> " +
rs.getString(2) + "</td><td> " +
rs.getString(3) + "</td><td> " +
rs.getDouble(4) + "</td>");
ctr = ctr + 1;
} catch (ClassNotFoundException cnfe) {
System.out.println("Error loading driver: "+cnfe);

Output of JDBC Example