declare the member variable to hold the connection reference
this begins our override of init()
delegate the work to the super class
start a try/catch block
load and register the JDBC/ODBC bridge as our JDBC driver
open a connection to the DSN
this exception might be thrown by the Class.forName() call
this message will appear on the console where you started the Web server
this exception might be thrown by the getConnection() call
the exception might be thrown by the getConnection() call
this message will appear on the console where you started the Web server"
begins our override of destroy()
start a try/catch block
close the database connection
the exception might be thrown by the close() call
this message will appear on the console where you started the Web server"
the remainder of the servlet was omitted for space reasons
The four key points in this code are:
Import the SQL classes so you can use Connection easily.
Make the connection reference a member variable of the servlet class.
Load the driver by creating the class. The name is always "sun.jdbc.odbc.JdbcOdbcDriver" if you are using the bridge.
Open the database connection by calling getConnection. The first parameter is always
"jdbc:odbc:YOURDSN" with YOURDSN replaced by the DSN to which you want to connect.
The second and third parameters specify an id and password.
Code and DSN tab
In the next lesson, you will learn how to read from a data source.