Sie sind auf Seite 1von 14

1

Database Application Development


SSK 3408
Chapter 5
Client-Server Application
Development
(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
administrator.
You can also use an ODBC driver (and so a
Windows DSN) if you have a JDBC-ODBC

Understanding JSP Connection


..cont.

JDBC

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


database

Disconnect from the database

classes12.jar &
nls_charset12.jar

Edit CLASSPATH
.;D:\app\Admin\product\11.1.0\client_1\jdbc\l
ib\classes12.jar;D:\app\Admin\product\11.
1.0\client_1\jdbc\lib\nls_charset12.jar

To Start up Orion Server

JDBC Detail Process

1. Load the driver


Class.forName(oracle.jdbc.driver.OracleDriver);

2. Define the Connection URL


String hostname = 10.101.100.13;
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 (rs.next()) {
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


cnn.close();

Basic JDBC Example


<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String hostname = 172.16.60.13";
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>");
out.println("<tr><th>No.</th><th>EmpID</th><th>FirstName</th><th>Salary</th><th>Commission</th></tr>");
while (rs.next()) {
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>");
out.println("</tr>");
ctr = ctr + 1;
}
conn.close();
} catch (ClassNotFoundException cnfe) {
System.out.println("Error loading driver: "+cnfe);
}
%>

Output of JDBC Example

Das könnte Ihnen auch gefallen