+ All Categories
Home > Documents > Java/JDBC

Java/JDBC

Date post: 12-Jan-2016
Category:
Upload: rae
View: 20 times
Download: 0 times
Share this document with a friend
Description:
Java/JDBC. Some Database terminology (brief) A simple stand alone JDBC Application Java Server Pages and Scoping A simple JSP and JDBC example JSP and JDBC Connection Pooling Summary. Some Database Terminolgy. Gary Alperson helped developed these slides and the JDBC example. - PowerPoint PPT Presentation
82
Java/JDBC • Some Database terminology (brief) • A simple stand alone JDBC Application • Java Server Pages and Scoping • A simple JSP and JDBC example • JSP and JDBC Connection Pooling • Summary
Transcript
Page 1: Java/JDBC

Java/JDBC

• Some Database terminology (brief)• A simple stand alone JDBC Application• Java Server Pages and Scoping• A simple JSP and JDBC example• JSP and JDBC Connection Pooling• Summary

Page 2: Java/JDBC

Some Database Terminolgy

Gary Alperson helped developed these slides and the JDBC example.

Page 3: Java/JDBC

Database Terminology

• Database: A shared collection of logically related data (and a description of this data) designed to meet the information needs of an organization

• Relation: A table with columns and rows

• Attribute: A named column of a relation

• Tuple: A row in a relation

Definitions from Database Systemsby Connolly, Begg, and Strachan

Page 4: Java/JDBC

Sample Table

brokerb_id lname fname

1 Smith John2 Jones Hannah3 Reynolds Leon4 Chang Donna5 Smith Deborah6 Thompson Daniel7 Frendun Laura

Page 5: Java/JDBC

brokerb_id lname fname

1 Smith John2 Jones Hannah3 Reynolds Leon4 Chang Donna5 Smith Deborah6 Thompson Daniel7 Frendun Laura

Attribute

Page 6: Java/JDBC

brokerb_id lname fname

1 Smith John2 Jones Hannah3 Reynolds Leon4 Chang Donna5 Smith Deborah6 Thompson Daniel7 Frendun Laura

Tuple

Page 7: Java/JDBC

SQL

• Data Definition Language (DDL)– Create tables– Modify tables– Delete (drop) tables

• Data Manipulation Language (DML)– Insert data– Update data– Select data

Page 8: Java/JDBC

Select Statement

brokerb_id lname fname

1 Smith John2 Jones Hannah3 Reynolds Leon4 Chang Donna5 Smith Deborah6 Thompson Daniel7 Frendun Laura

We will use this data for our examples

Page 9: Java/JDBC

From the broker table, select the contents of the last name attribute

Query

SELECT lname

FROM broker;

ResultslnameSmithJonesReynoldsChangSmithThompsonFrendun

SQL is not case sensitive. Key SQL words are capitalized and line breaks are inserted by convention.

Page 10: Java/JDBC

From the broker table, select all attributes

Query

SELECT *

FROM broker;

Results

* Acts as a wildcard

brokerb_id lname fname

1 Smith John2 Jones Hannah3 Reynolds Leon4 Chang Donna5 Smith Deborah6 Thompson Daniel7 Frendun Laura

Page 11: Java/JDBC

From the broker table, select all attributes where the last name is Smith

Query

SELECT *

FROM broker

WHERE lname = ‘Smith’;

Results

•Note that the string is enclosed by single quotes•The contents of a string are case sensitive

brokerb_id lname fname

1 Smith John5 Smith Deborah

Page 12: Java/JDBC

Use AND or OR to connect multiple where clauses

Query

SELECT *FROM brokerWHERE lname = ‘Smith’AND fname = ‘John’;

Results

b_id lname fname1 Smith John

Page 13: Java/JDBC

Example with two Tables

One-to-many relationship•Each broker may have many customers•Each customer is only affiliated with one broker•The b_id joins both tables by identifying the unique broker that each customer is associated with

broker customerb_id lname fname

1 Smith John2 Jones Hannah3 Reynolds Leon4 Chang Donna5 Smith Deborah6 Thompson Daniel7 Frendun Laura

customerid b_id lname fname

1 1 LeParc Wilson2 1 AnstinceDevon3 2 Tabor Mark4 2 Lenks Sandy5 2 PhillipsonRichard6 3 Kini Raghu7 4 Kim David

Page 14: Java/JDBC

Cartesian Productbroker.b_id

broker.lname

broker.fname

id customer.b_id

broker.lname

broker.fname

1 Smith John 1 1 LeParc Wilson1 Smith John 2 1 Anstince Devon1 Smith John 3 2 Tabor Mark1 Smith John 4 2 Lenks Sandy1 Smith John 5 2 Phillipson Richard1 Smith John 6 3 Kini Raghu1 Smith John 7 4 Kim David2 Jones Hannah 1 1 LeParc Wilson2 Jones Hannah 2 1 Anstince Devon2 Jones Hannah 3 2 Tabor Mark2 Jones Hannah 4 2 Lenks Sandy2 Jones Hannah 5 2 Phillipson Richard2 Jones Hannah 6 3 Kini Raghu2 Jones Hannah 7 4 Kim David3 Reynolds Leon 1 1 LeParc Wilson3 Reynolds Leon 2 1 Anstince Devon3 Reynolds Leon 3 2 Tabor Mark3 Reynolds Leon 4 2 Lenks Sandy3 Reynolds Leon 5 2 Phillipson Richard3 Reynolds Leon 6 3 Kini Raghu3 Reynolds Leon 7 4 Kim David4 Chang Donna 1 1 LeParc Wilson4 Chang Donna 2 1 Anstince Devon4 Chang Donna 3 2 Tabor Mark4 Chang Donna 4 2 Lenks Sandy4 Chang Donna 5 2 Phillipson Richard4 Chang Donna 6 3 Kini Raghu4 Chang Donna 7 4 Kim David

When you do a query on multiple tables, SQL begins by creating the Cartesian product, which combines each tuple from one relation from every tuple of the other relation.(Actual SQL implementationsare free to compute the resulting table efficiently,i.e., the actual Cartesian product may not be generated at all.)

Page 15: Java/JDBC

Query

SELECT *

FROM customer, broker

WHERE broker.b_id = 1;

SQL does not realize that the b_id in the customer table is the same as the b_id in the broker table unless you join them in the

where clause.

broker.b_id

broker.lname

broker.fname

id customer.b_id

broker.lname

broker.fname

1 Smith John 1 1 LeParc Wilson1 Smith John 2 1 Anstince Devon1 Smith John 3 2 Tabor Mark1 Smith John 4 2 Lenks Sandy1 Smith John 5 2 Phillipson Richard1 Smith John 6 3 Kini Raghu1 Smith John 7 4 Kim David

Results

Page 16: Java/JDBC

Cartesian ProductQuery

SELECT *

FROM customer, broker

WHERE broker.b_id = 1

AND broker.b_id = customer.b_id;

Resultsbroker.b_id

broker.lname

broker.fname

id customer.b_id

broker.lname

broker.fname

1 Smith John 1 1 LeParc Wilson1 Smith John 2 1 Anstince Devon

Page 17: Java/JDBC

ODBC

ODBC is a programming interface that enables applications to access data in

database systems that use Structured Query Language (SQL) as a data standard.

Page 18: Java/JDBC

Creating an ODBC Connection

• Click on the Start button.• Choose Settings, Control Panel• Double-click on ODBC Data Sources• Choose the System DSN tab• Click Add

Page 19: Java/JDBC

•Click on the desired driver (MSAccess)•Click on the Finish button

Page 20: Java/JDBC

•Enter a Data Source Name•Click on the Select button•Locate the desired file or directory•Click OK

Page 21: Java/JDBC

Java’s JDBC

• Allows access to any ANSI SQL-2 DBMS• Does its work in terms of SQL• The JDBC has classes that represent: database connections SQL Statements Result sets database metadata• Can be connected to ODBC

Page 22: Java/JDBC

SQL Query as a Java String

The SQL

SELECT customer.lname

FROM customer, broker

WHERE broker.lname = ‘Smith’

AND broker.b_id <> 1

AND broker.b_id = customer.b_id;

From both tables select the last names of all customers whose broker’s last name is Smith but whose broker ID is not 1.

Page 23: Java/JDBC

Executing a query in Java

// Statement aStatement = statement got from connection

String last = “Smith”;

int nonID = 1;

String q = “SELECT customer.lname FROM customer, broker” + “WHERE broker.lname = \’” + last + “\’ AND broker.b_id” + “<>” + nonID + “AND broker.b_id = customer.b_id;”);ResultSet rs = aStatement.executeQuery(q);

•The slash (\) is the escape character. It precedes the single quote to tell Java to include that quote in the String•The String last is outside of the double quotes, because it must be concatonated with the String sent to the database, but it falls within the single quotes so that SQL treats it as a string•nonID does not go within single quotes since it is numeric•Since the String is an SQL statement, it uses = and <> rather than == and !=

Page 24: Java/JDBC

A Simple Standalone JDBC Application

// This program makes use of a stock database// and the primary JDBC classes (Connection, Statement, // ResultSet and ResultSetMetaData)

import java.util.*;import java.sql.*;import java.io.*;

public class TestCoolStocksDB {

public static void main(String args[]) {

Connection con = null; Statement s = null;

Page 25: Java/JDBC

ResultSet rs = null; ResultSetMetaData rsm = null; String answer = "";

try { DriverManager.registerDriver( new sun.jdbc.odbc.JdbcOdbcDriver()); con = DriverManager.getConnection("jdbc:odbc:CoolStocks");

s = con.createStatement(); rs = s.executeQuery("select * from customer"); rsm = rs.getMetaData();

Page 26: Java/JDBC

while(rs.next()) { for(int col = 1; col <= rsm.getColumnCount(); col++) answer += rs.getString(col); } con.close(); } catch (SQLException sqle) { System.err.println("Exception caught in main:" + sqle); } System.out.println(answer); }}

Page 27: Java/JDBC

It WorksD:\McCarthy\www\95-713\examples\jdbc>java TestCoolStocksDB

1JonesRobert2SmithElaine3ChanJane4MoralesHector5SchwartzMichael

The carriage returns were added.

Page 28: Java/JDBC

JSP and Scoping

• When a browser visits a web site we may need to know if that same browser has visited

before.

• Java provides page scope, request scope, session scope, and application scope

• For long term persistence we will often need a

databaseMuch of this lecture is from a nice littlebook entitled “Pure JSP” by Goodwill publishedby SAMS

Page 29: Java/JDBC

Page Scope

Beans with page scope are accessible only

within the page where they were created.

A bean with page-level scope is not

persistent between requests or outside the

page

Page 30: Java/JDBC

Page Scope Example/* A simple bean that counts visits. */import java.io.*;

public class Counter implements Serializable { private int count = 1; public Counter() {} public int getCount() { return count++; } public void setCount(int c) { count = c; }}

Page 31: Java/JDBC

Under Tomcat

webapps

myApplication

WEB-INF

classes web.xml

Counter.java

SomeFile.jsp

These programs require a container.

Page 32: Java/JDBC

<%-- Use the Counter bean with page scope. --%><%-- The Counter class must be imported. Its in the WEB-INF/classes directory --%>

<%@ page import="Counter" %> <jsp:useBean id = "ctr" scope = "page" class = "Counter" /><html> <head> <title>Page Bean Example</title> </head> <body> <h3>Page Bean Example </h3> <center> <b>The current count for the counter bean is: </b> <jsp:getProperty name = "ctr" property ="count" /> </center> </body></html>

Page 33: Java/JDBC

The count never changes.

Page 34: Java/JDBC

One Page May Call Another

<%-- Caller page Caller.jsp --%>

<html> <head> <title>Caller page </title> </head> <body> <h1> Caller page </h1> <jsp:forward page = "Callee.jsp" /> </body></html>

Any response data is cleared and controlpasses to the new page.

Page 35: Java/JDBC

Callee.jsp

<%-- Callee page --%>

<html> <head> <title>Callee page </title> </head> <body> <h1> Callee page </h1> </body></html>

Page 36: Java/JDBC

After Visiting Caller.jsp

Page 37: Java/JDBC

Request Scope

• One page may call another and the bean is still available.• Its considered one request.• The second page will use an existing bean before creating a new one.• When the current request is complete the bean is reclaimed by the JVM.

Page 38: Java/JDBC

Request Scope Caller.jsp<%-- Caller page --%><%@ page import="Counter" %>

<jsp:useBean id = "ctr" scope = "request" class = "Counter" /><html> <head> <title>Caller page </title> <jsp:setProperty name = "ctr" property = "count" value = "10" /> </head> <body> <h1> Caller page </h1> <jsp:forward page = "Callee.jsp" /> </body></html>

Page 39: Java/JDBC

Request Scope Callee.jsp

<%-- Callee page --%><%@ page import="Counter" %> <jsp:useBean id = "ctr" scope = "request" class = "Counter" /><html> <head> <title>Callee page </title> </head> <body> <h1> Callee page </h1> <jsp:getProperty name = "ctr" property ="count" /> </body></html>

Page 40: Java/JDBC

After Visiting Caller.jsp

Page 41: Java/JDBC

Session Scope

Beans with session scope are accessible within pages processingrequests that are in the same session as the one in which thebean was created.

Session lifetime is typically configurable and is controlled bythe servlet container. Currently, my session ends when thebrowser exits.

Multiple copies of the same browser each get their own session bean.

Page 42: Java/JDBC

Session Scope Example<%-- SessionBeanPage.jsp --%><%@ page import="Counter" %> <jsp:useBean id = "ctr" scope = "session" class = "Counter" /><html> <head> <title>Session Bean Page </title> </head> <body> <h1> Session Bean Page </h1> <B>Visit number <jsp:getProperty name = "ctr" property = "count"/> </B> </body></html>

Page 43: Java/JDBC

Session Scope Example

The counter increments on each hit till browser exits. New browserback to 1.

Page 44: Java/JDBC

Application Beans

A bean with a scope value of application has an even broaderand further reaching availability than session beans.

Application beans exist throughout the life of the JSP containeritself, meaning they are not reclaimed until the server is shutdown.

Session beans are available on subsequent requests from the samebrowser. Application beans are shared by all users.

Page 45: Java/JDBC

Application Bean Example 1<%-- ApplicationBeanPage1.jsp --%><%@ page import="Counter" %> <jsp:useBean id = "ctr" scope = "application" class = "Counter" /><html> <head> <title>Application Bean Page </title> </head> <body> <h1> Application Bean Page </h1> <B>Visit number <jsp:getProperty name = "ctr“ property = "count"/> </B> </body></html>

Page 46: Java/JDBC

Application Bean Example 2<%-- ApplicationBeanPage2.jsp --%><%@ page import="Counter" %>

<jsp:useBean id = "ctr" scope = "application" class = "Counter" /><html> <head> <title>Application Bean Page Two </title> </head> <body> <h1> Application Bean Page Two </h1> <B>Visit number <jsp:getProperty name = "ctr“ property = "count"/> </B> </body></html>

Page 47: Java/JDBC

After several visits with IE5 we visit with Netscape.

Page 48: Java/JDBC

After visiting from a different machines with a different browsers,we still keep count.

Page 49: Java/JDBC

A Simple JSP/JDBC Example

stocks.mdb database schema

customer stocks portfolioid symbol idlname company symbolfname price num_shares

There are three tables. Both customer and stocks have a one-to-many relationship with portfolios. The database stocks.mdbwas registered with the ODBC driver as “CoolStocks”

Page 50: Java/JDBC

Register w/ODBCCreate an ODBC data source.Click on the Start button.Choose Settings, Control PanelDouble-click on ODBC Data SourcesChoose the System DSN tabClick AddClick on the desired driver (MSAccess)Click on the Finish buttonEnter a Data Source Name (I called my database CoolStocksand that name appears in the java code below)Click on the Select buttonLocate the directory and file containing your database. This will bethe “stock.mdb” file created by Microsoft Access.Click OK

Page 51: Java/JDBC

A Simple JSP/JDBC Example

<TITLE>JSP JDBC Example 1</TITLE></HEAD>

<BODY><!– Adapted from James Goodwill’s Pure JSP <!-- Set the scripting language to java and --><!-- import the java.sql package --><%@ page language="java" import="java.sql.*" %><%@ page import= "java.io.*" %>

Page 52: Java/JDBC

<% Connection con = null; try { // Load the Driver class file Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// Make a connection to the ODBC datasource Movie Catalog con = DriverManager.getConnection("jdbc:odbc:CoolStocks");

// Create the statement Statement statement = con.createStatement();

// Use the created statement to SELECT the DATA // FROM the customer Table. ResultSet rs = statement.executeQuery("SELECT * " + "FROM customer"); // Iterate over the ResultSet %>

Page 53: Java/JDBC

<!-- Add an HTML table to format the results --> <TABLE BORDER="1"> <TR> <TH> Customer - ID</TH><TH>Last Name</TH> <TH>First Name</TH> <% while ( rs.next() ) {

// get the id, convert to String out.println("<TR>\n<TD>" + rs.getString("id") + "</TD>");

// get the last name out.println("<TD>" + rs.getString("lname") + "</TD>");

// get the first name out.println("<TD>" + rs.getString("fname") + "</TD>\n</TR"); }

Page 54: Java/JDBC

// Close the ResultSet rs.close(); } catch (IOException ioe) { out.println(ioe.getMessage()); } catch (SQLException sqle) { out.println(sqle.getMessage()); } catch (ClassNotFoundException cnfe) { out.println(cnfe.getMessage()); } catch (Exception e) { out.println(e.getMessage()); }

Page 55: Java/JDBC

finally { try { if ( con != null ) {

// Close the connection no matter what con.close(); } } catch (SQLException sqle) {

out.println(sqle.getMessage()); } }

%></BODY></HTML>

Page 56: Java/JDBC

It Works!

Page 57: Java/JDBC

An Example Using Connection Pooling

The example above opens a connection every timethere is a visit.

Goodwill presents another approach in chapter 14.

Page 58: Java/JDBC

PooledConnection.java

// Adapted from Goodwill's Pure JSPimport java.sql.*;

public class PooledConnection {

// Real JDBC Connection private Connection connection = null; // boolean flag used to determine if connection is in use private boolean inuse = false;

Page 59: Java/JDBC

// Constructor that takes the passed in JDBC Connection // and stores it in the connection attribute. public PooledConnection(Connection value) { if ( value != null ) { connection = value; } }

// Returns a reference to the JDBC Connection public Connection getConnection() { return connection; }

Page 60: Java/JDBC

// Set the status of the PooledConnection. public void setInUse(boolean value) { inuse = value; } // Returns the current status of the PooledConnection. public boolean inUse() { return inuse; } // Close the real JDBC Connection public void close() { try { connection.close(); } catch (SQLException sqle) { System.err.println(sqle.getMessage()); } }}

Page 61: Java/JDBC

ConnectionPool.java

// Adapted from James Goodwill's Pure Java

import java.sql.*;import java.util.*;

public class ConnectionPool {

// JDBC Driver Name private String driver = null; // URL of database private String url = null; // Initial number of connections. private int size = 0;

Page 62: Java/JDBC

// Username private String username = new String(""); // Password private String password = new String(""); // Vector of JDBC Connections private Vector pool = null;

public ConnectionPool() {

}

// Set the value of the JDBC Driver public void setDriver(String value) { if ( value != null ) { driver = value; } }

Page 63: Java/JDBC

// Get the value of the JDBC Driver public String getDriver() { return driver; }

// Set the URL Pointing to the Datasource public void setURL(String value ) { if ( value != null ) { url = value; } }

// Get the URL Pointing to the Datasource public String getURL() { return url; }

Page 64: Java/JDBC

// Set the initial number of connections public void setSize(int value) { if ( value > 1 ) { size = value; } }

// Get the initial number of connections public int getSize() { return size; } // Set the username public void setUsername(String value) { if ( value != null ) { username = value; } }

Page 65: Java/JDBC

// Get the username public String getUserName() { return username; }

// Set the password public void setPassword(String value) { if ( value != null ) { password = value; } } // Get the password public String getPassword() { return password; }

Page 66: Java/JDBC

// Creates and returns a connection private Connection createConnection() throws Exception {

Connection con = null;

// Create a Connection con = DriverManager.getConnection(url, username, password);

return con; }

Page 67: Java/JDBC

// Initialize the pool public synchronized void initializePool() throws Exception {

// Check our initial values if ( driver == null ) { throw new Exception("No Driver Name Specified!"); } if ( url == null ) { throw new Exception("No URL Specified!"); } if ( size < 1 ) { throw new Exception("Pool size is less than 1!"); }

Page 68: Java/JDBC

// Create the Connections try { // Load the Driver class file Class.forName(driver); // Create Connections based on the size member for ( int x = 0; x < size; x++ ) { Connection con = createConnection(); if ( con != null ) { // Create a PooledConnection to encapsulate the // real JDBC Connection PooledConnection pcon = new PooledConnection(con); // Add the Connection to the pool. addConnection(pcon); } } }

Page 69: Java/JDBC

catch (Exception e) { System.err.println(e.getMessage()); throw new Exception(e.getMessage()); } } // Adds the PooledConnection to the pool private void addConnection(PooledConnection value) { // If the pool is null, create a new vector // with the initial size of "size" if ( pool == null ) { pool = new Vector(size); } // Add the PooledConnection Object to the vector pool.addElement(value); }

Page 70: Java/JDBC

public synchronized void releaseConnection(Connection con) {

// find the PooledConnection Object for ( int x = 0; x < pool.size(); x++ ) {

PooledConnection pcon = (PooledConnection)pool.elementAt(x); // Check for correct Connection if ( pcon.getConnection() == con ) {

System.err.println("Releasing Connection " + x); // Set its inuse attribute to false, which // releases it for use pcon.setInUse(false); break; } } }

Page 71: Java/JDBC

// Find an available connection public synchronized Connection getConnection() throws Exception { PooledConnection pcon = null; // find a connection not in use for ( int x = 0; x < pool.size(); x++ ) { pcon = (PooledConnection)pool.elementAt(x);

// Check to see if the Connection is in use if ( pcon.inUse() == false ) {

// Mark it as in use pcon.setInUse(true); // return the JDBC Connection stored in the // PooledConnection object return pcon.getConnection(); } }

Page 72: Java/JDBC

// Could not find a free connection socreate and add a new one try { // Create a new JDBC Connection Connection con = createConnection(); // Create a new PooledConnection, passing it the JDBC Connection pcon = new PooledConnection(con); // Mark the connection as in use pcon.setInUse(true); // Add the new PooledConnection object to the pool pool.addElement(pcon); } catch (Exception e) { System.err.println(e.getMessage()); throw new Exception(e.getMessage()); } // return the new Connection return pcon.getConnection(); }

Page 73: Java/JDBC

// When shutting down the pool, you need to first empty it. public synchronized void emptyPool() {

// Iterate over the entire pool closing the // JDBC Connections. for ( int x = 0; x < pool.size(); x++ ) {

System.err.println("Closing JDBC Connection " + x);

PooledConnection pcon = (PooledConnection)pool.elementAt(x);

// If the PooledConnection is not in use, close it if ( pcon.inUse() == false ) { pcon.close(); }

Page 74: Java/JDBC

else {

// If it is still in use, sleep for 30 seconds and // force close. try {

java.lang.Thread.sleep(30000); pcon.close(); } catch (InterruptedException ie) {

System.err.println(ie.getMessage()); } } } }}

Page 75: Java/JDBC

JDBCPooledExample.jsp

<html> <body><%@ page errorPage="errorpage.jsp" %><%@ page import="java.util.*" %><%@ page import="java.sql.*" %><%@ page import= "java.io.*" %><%@ page import="ConnectionPool" %>

<!-- Instantiate the ConnectionPool bean with an id of "pool" --><jsp:useBean id="pool" scope="application" class="ConnectionPool" />

Page 76: Java/JDBC

<% Connection con = null; try { // The pool is not initialized if ( pool.getDriver() == null ) { // initialize the pool pool.setDriver("sun.jdbc.odbc.JdbcOdbcDriver"); pool.setURL("jdbc:odbc:CoolStocks"); pool.setSize(5); pool.initializePool(); } // Get a connection from the ConnectionPool con = pool.getConnection(); // Create the statement Statement statement = con.createStatement();

Page 77: Java/JDBC

// Use the created statement to SELECT the DATA // FROM the customer Table. ResultSet rs = statement.executeQuery("SELECT * " + "FROM customer");

// Iterate over the ResultSet %> <!-- Add an HTML table to format the results --> <center> <table border="1" cellspacing="0" cellpadding="2"width="500"> <tr> <TH> Customer - ID</TH><TH>Last Name</TH> <TH>First Name</TH>

Page 78: Java/JDBC

<% while ( rs.next() ) {

// get the id, convert to String out.println("<TR>\n<TD>" + rs.getString("id") + "</TD>"); // get the last name out.println("<TD>" + rs.getString("lname") + "</TD>"); // get the first name out.println("<TD>" + rs.getString("fname") + "</TD>\n</TR"); } // Close the ResultSet rs.close(); out.println("</table></center>"); } catch (IOException ioe) { out.println(ioe.getMessage()); }

Page 79: Java/JDBC

catch (SQLException sqle) { out.println(sqle.getMessage()); } catch (ClassNotFoundException cnfe) { out.println(cnfe.getMessage()); } catch (Exception e) { out.println(e.getMessage()); } finally { try { if ( con != null ) { // release the connection no matter what pool.releaseConnection(con); } }

Page 80: Java/JDBC

catch (Exception e) {

out.println(e.getMessage()); } }%> </body></html>

Page 81: Java/JDBC

It works too!

Page 82: Java/JDBC

Summary

With JDBC we can:

Write standalone programs that interact with RDBMS.

Write server side code that interacts with server side RDBMS.

We have not covered the many possible uses of SOAP clientsinteracting with SOAP servers utilizing back end RDBMS.


Recommended