Here is code snipet for you:
public class BaseServlet extends HttpServlet {
static Connection con;
public void init(ServletConfig config) throws ServletException {
super.init(config);
if (con == null) {
String url = "jdbc:odbc:<odbc_name>";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, "", "");
}
catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
}
public void destroy() {
if (con != null) {
try {
con.close();
}
catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}
}
Note: this is not the best way to store connection to static field.
Dalibor
______________________________ Reply Separator _________________________________
Subject: How to make a database connection in a servlet ?
Author: [EMAIL PROTECTED] (Andreas Horchler
<[EMAIL PROTECTED]>) at INTERNET
Date: 31.7.2001 2:16
Hi,
I am running Apache 1.3.20, Tomcat 3.2.3 and JDK 1.3.1 from Sun on
Windows NT and
everything is working fine.
Because these are my first experiences with a web server, I had a look
at several samples
delivered with Tomcat.
But how will I be able to connect from a sevlet to an Oracle 8.1.6
database server
(not running on the local machine) ?
Do I have to use the RequestInterceptor JDBCRealm declared in the
serverl.xlm file ?
On the machine running the Apache and Tomcat I have an Oracle client
8.1.7 installed.
Can anyone explain to me the steps I have to follow, and explain how
they work ?
(a few lines of sample code would be very nice)
Thanks in advance and regards
Andreas