database ssk3408-chapter05

14
1 Chapter 5 Chapter 5 Client-Server Client-Server Application Application Development Development (2-Tier Architecture) (2-Tier Architecture) tabase Application Developme tabase Application Developme K 3408 K 3408

Upload: rockers91

Post on 18-Jul-2016

40 views

Category:

Documents


1 download

DESCRIPTION

Database

TRANSCRIPT

Page 1: Database SSK3408-Chapter05

1

Chapter 5Chapter 5Client-Server Client-Server Application Application

Development Development (2-Tier Architecture)(2-Tier Architecture)

Database Application DevelopmentDatabase Application DevelopmentSSK 3408SSK 3408

Page 2: Database SSK3408-Chapter05

Interfacing with the Database

The same table open with Notepad:

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.

Page 3: Database SSK3408-Chapter05

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 vendor’s documentation or consult your system administrator.

You can also use an ODBC driver (and so a Windows DSN) if you have a JDBC-ODBC Bridge driver.

Page 4: Database SSK3408-Chapter05

Understanding JSP Connection ..cont.

Page 5: Database SSK3408-Chapter05

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 low-level 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

Page 6: Database SSK3408-Chapter05

classes12.jar & nls_charset12.jar

Page 7: Database SSK3408-Chapter05

Edit CLASSPATH.;D:\app\Admin\product\11.1.0\client_1\

jdbc\lib\classes12.jar;D:\app\Admin\product\11.1.0\client_1\jdbc\lib\nls_charset12.jar

Page 8: Database SSK3408-Chapter05

To Start up Orion Server

Page 9: Database SSK3408-Chapter05

JDBC Detail Process 1. Load the driver

2. Define the Connection URL

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

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

Page 10: Database SSK3408-Chapter05

3. Establish the Connection

4. Create a Statement

5. Execute a Query

JDBC Detail Process ..cont.

String username = “salmi”;String password = “secret”;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);

Page 11: Database SSK3408-Chapter05

6. Process the Result

7. Close the Connection

JDBC Detail Process ..cont.

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));}

cnn.close();

Page 12: Database SSK3408-Chapter05

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);

Page 13: Database SSK3408-Chapter05

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); } %>

Page 14: Database SSK3408-Chapter05

Output of JDBC Example