Database Connectivity Using JSP

What is JSP ?
• JSP consists of two parts………. 1) Template data that means HTML 2) Elements - Java Specifications (As we are going to interact with databases so Java specifications will be of JDBC API.)

Elements
1) Directives 2) Scripting Elements 3) Standard Actions

Directives
• Directive are used to set global values such as class declaration, method implementation, output content type etc. • They do not produce any output to the client and their scope is entire page. • Three types are there1) Page Directive 2) Include Directive

Scripting Elements
• JSP Scripting elements allow Java Code , Variable or Method Declaration in our JSP pages. • Three types are there1) Declarations <%!.............................%> 2)Scriptlets 3) Expressions

Scriptlets
• A scriptlet is a block of Java Code that is executed during the request processing time and is enclosed between % to %. • • • • <%for(int i=0;i<10;i++) { …….. }%>

Expressions
• <%=“This JSP page is accessed “+i+”times%>

What Is JDBC ?
• JDBC (Java DataBase Connectivity) is an API for accessing relational databases from different database vendors in a consistent way

Database Drivers
• Think of a database as just another device connected to your computer • like other devices it has a driver program to relieves you of having to do low level programming to use the database • The driver provides you with a high level API to the database

Imp. Points
– The driver converts calls from JDBC API to vendor’s API – The same Java application can be used with a different vendor’s database by simply switching JDBC driver and changing one line of Java code.

JDBC Driver Types
• JDBC database drivers are classified into four types based on the nature of the driver provided
– Bridges. Convert from JDBC to another standard for example ODBC. That standard then converts into the vendor specific API. – Part Java part Native. Driver contains some Java code and some Native code – Intermediate Server. JDBC talks directly to a separate server which then converts from JDBC to native API (great for intranets) – Pure Java. JDBC driver converts directly to native API and sends through vendor specific network protocol (great for intranets)

ODBC
• Open Data Base Connectivity • Developed by Microsoft for the Windows platform as the way for Windows applications to access Microsoft databases (SQL Server, FoxPro, Access) • Has become an industry standard • Most data base vendors supply native, odbc, and jdbc drivers for their data base products

JDBC COMPARED TO ODBC
Generally speaking, JDBC is to Java what Microsoft's Open Database Connectivity (ODBC) interface is to the C language. Both JDBC and ODBC: • Provide a vendor-independent API that allows the same application to connect to different vendors' databases and retrieve and update data using standard SQL statements. • Adopt the architecture of imposing a driver manager between applications and vendorsupplied drivers that translate between the standard API

JDBC COMPARED TO ODBC Cont..
• JDBC applications enjoy the platformindependence of Java, which lends itself to Internet applications. ODBC applications must, at a minimum, be recompiled to run on a different operating-system/hardware combination. • JDBC does not require software on each client system, which also recommends it for Internet applications. • JDBC is much simpler and easier to learn than ODBC. • JDBC is not primarily targeted for PC application

JDBC Architecture
Java Application

JDBC API

Data Base Drivers

Access

SQL Server

DB2

Informix

MySQL

Sybase

JDBC Driver Types
• Type 1
– JDBC-ODBC Bridge

• Type 2
– Native API, partially java

• Type 3
– JDBC Network Driver, partially java

• Type 4
– 100% Java

Type 1 Drivers
• Translate JDBC into ODBC and use Windows ODBC built in drivers

• Driver sun.jdbc.odbc.JdbcOdbc provided by JavaSoft with JDK

Type 1 Driver (cont.)

Client

JDBCODBC

ODBC

DB

Vendor Library

Type 2 Drivers
• Converts JDBC to data base vendors native SQL calls • like Type 1 drivers; requires installation of binaries on each client

Type 2 Drivers (cont.)

Client

JDBC

DB

Vendor Library

Type 3 Drivers
• Translates JDBC to a DBMS independent network protocol • Typically talks directly with a middleware product which in turn talks to the RDBMS
– Jaguar, DBAnywhere, SequeLink

• Most flexible driver type • all java

Type 3 Drivers (cont.)

Client

JDBC

Vendor Middleware

DB

Tier 1

Tier 2

Tier 3

Type 4 Drivers
• Converts JDBC directly to native API used by the RDBMS • compiles into the application , applet or servlet; doesn’t require anything to be installed on client machine, except JVM • handiest driver type

Type 4 Drivers (cont.)

Client

JDBC

DB

Package Used
• We will use a package “java.sql”

Steps in Using JDBC
• • • • • • • • 1. 2. 3. 4. 5. 6. 7. 8. Load the JDBC driver Define the Connection URL Establish the Connection Make a Query Create a Statement object Execute a query Process the results Close the connection

JDBC: Details of Process
1. Load the driver

Class.forName("sun.jdbc.odbc.JdbcOdbcDri ver");

2. Define the Connection URL & Establish a Connection
• Connection con =
DriverManager.getConnection("jdbc:odbc:gau rav");

4) Make A Query
• String sql="insert into employeedetail values(22,‘riti')";

5) Create a Statement
• Statement stmt = con.createStatement();

Types of Statement
• Statement- We have discussed • Prepared Statement- To Execute precompiled Query • Callable Statement- To Invoke Stored Procedure

6)Execute the Query (With the help of Statement Object )

stmt.execute(sql);

7) Close the resourses
stmt.close(); con.close();

• A sample JSP Program

Session Tracking
• Applications in use today need to keep at least some constant information about the previous transactions.

Session tracking example
• In this example we take User’s name on a page • Transfer it to another page and take age on that page • Transfer name and age to third page and take email id on this page • Display all these things on the last page

• Session Tracking Example

Sign up to vote on this title
UsefulNot useful