::-----Original Message-----
::From: A mailing list about Java Server Pages specification and reference
::[mailto:[EMAIL PROTECTED]]On Behalf Of steve Gale
::Sent: Tuesday, March 23, 1999 11:52 AM
::To: [EMAIL PROTECTED]
::Subject: Connecting JSP to an ODBC database
::
::
::Hi,
::Bieing new to both JSP servlets and beans, but with experience in
::both java and ASP
::I am searching for a way to connect to an ODBC database from JSP.
::Is a bean required or can this be done using the <servlet... tag,
::which is preffered and why.

A bean can be used to encapsulate the database routines, or you can just
embed the Java code with <% %> pairs, or you can call a servlet which
performs the database processing. Which is better? As with anything, depends
on what you're doing. A bean can be nice as it encapsulates your db code
into an object. Embedding within <% %> is ugly, as it mingles presentation
of the database data with the actual coding to retrieve the data i.e. pain
in the butt to manage or scale. Servlets also are fine as they are nice
encapsulations and you may already have servlets that do this kind of stuff
floating around...

Anyway, the important steps when using JDBC follow (search the net for this
though, there's tons of documentation around for those willing to look):

1) Instantiate an instance of the JDBC driver you're trying to use
(jdbc-odbc bridge name from memory so pls check):

Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ).newInstance();

This determines if the class is available and instantiates a new instance of
it, making it available for the next step.

2) Ask the DriverManager for a Connection object based on the JDBC URL you
are using:

Connection      connDB = DriverManager.getConnection( "jdbc:odbc:MyDSN",
"username", "password" );

DriverManager searches through any registered drivers (instantiating a new
instance above is enough to register a driver with the DriverManager, as
each implementation is required to) and, based on the JDBC URL you are
using, returns the appropriate implementation of Connection.

3) Create a Statement object to retrieve a ResultSet

Statement       smentDB = connDB.createStatement();
ResultSet       rsDB = connDB.executeQuery( "SELECT * FROM Foo" );
or
rsDB = connDB.executeUpdate( "UPDATE Foo SET Bar = NULL" );

4) Close down connections to free resources:

rsDB.Close();
smentDB.Close();
connDB.Close();

Note smentDB.Close() closes the rsDB object, and connDB will close smentDB,
cascading down, so you can really just: connDB.Close(). Also note there's no
exception handling given here.

This has been brief but HTH,
Joe.

::I would appreciate a simple code
::example or a pointer in the right direction, It would also be a
::good inclusion in FAQ I believe,
::
::Regards,
::Steve Gale
::
::==================================================================
::=========
::To unsubscribe, send email to [EMAIL PROTECTED] and include
::in the body
::of the message "signoff JSP-INTEREST".  For general help, send email to
::[EMAIL PROTECTED] and include in the body of the message "help".
::


--
Joe Shevland
Turnaround Solutions Pty. Ltd.
mailto:[EMAIL PROTECTED]
http://www.TurnAround.com.au

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to