> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, July 31, 2001 11:50 AM
> To: [EMAIL PROTECTED]
> Subject: Re: How to make a database connection in a servlet ?
> 
> 
> 
> Here is code snipet for you:

Don't forget to import:

import java.sql.*;

import javax.sql.*; // if you need advance JDBC features (see
$J2EE_HOME/doc/api/javax/sql/package-summary.html)

import oracle.jdbc.driver.*; // for Oracle extensions to JDBC
import oracle.sql.*;         // for Oracle extensions to SQL

> 
>      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, "", "");

Or, if you're using original Oracle driver (recommended):

// url is of format "jdbc:oracle:<driver_type>:@"
// where driver_type can be thin, oci7, oci8, etc. (see docs for details)
String url = "jdbc:oracle:oci8:@";
Class.forName("oracle.jdbc.driver.OracleDriver");
java.util.Properties props = new java.util.Properties();
props.put ("database", "myhost:1521:testdb"); //<hostname>:<TCP/IP
port>:<Oracle SID>
props.put ("user", "scott");
props.put ("password", "tiger");
con = DriverManager.getConnection(url, props);



>            }
>            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
> 
> 
> 

Reply via email to