Re: Accessing context parameters from web.xml in java class



On Sep 29, 7:38 pm, Lew <l...@xxxxxxxxxxxxx> wrote:
Sameer wrote:
import java.sql.*;
import javax.servlet.*;

public class DBUtils {
public static Connection getDBConnection(ServletContext context)
throws Exception {
String driver = context.getInitParameter("db_driver");
String server = context.getInitParameter("db_server");
String port = context.getInitParameter("db_port");
String sid = context.getInitParameter("db_sid");
String conn_url = "jdbc:oracle:thin:@" + server + ":" + port + ":"
+ sid;
String db_username = context.getInitParameter("db_username");
String db_password = context.getInitParameter("db_password");
Class.forName(driver).newInstance();
Connection con = DriverManager.getConnection(conn_url, db_username,
db_password);
return con;
}

}

The happy-path part of this is more or less correct there, but for a few
points of note.

You don't need the throwaway instance of the DB driver, so Class.forName()
alone is enough, and you only ever need to load the driver once, not with
every connection. Consider using the generic version of Class.

You absolutely must not omit exception handling in production code. Never
declare a method simply "throws Exception"; use a custom exception. Always
handle and log exceptions. Log them at the point of occurrence. It even sets
a bad example to post Usenet code that violates these precepts unless you
include a disclaimer.

Use spaces, not TABs, to indent Usenet posts.

--
Lew- Hide quoted text -

- Show quoted text -

Thanks for the suggestions.
What do you mean by Generic version of class? Accepting data types as
parameters?
Please clarify.

.



Relevant Pages

  • get your potentially resuming element on board my squad
    ... exclusive era. ... require a transmission! ... it eases a contest too positive in connection with her ...
    (sci.crypt)
  • Re: SF: Refresher course
    ... All instant diplomatic house substitutes computers ... Hey, I'll decline the german. ... in connection with me it's ... access after Virginia blocks the strict kingdom's exception? ...
    (sci.crypt)
  • Re: Strange sudden shutdown issue with CF app
    ... Phone devices are notoriously low on virtual memory right out of the box because the OEMs decide to put every little thing into its own DLL and then have 25 processes launched at boot. ... Chris Tacke, Embedded MVP ... We have created an app that basically connects to a web service gets data, then stores it locally on the device, and when a record needs to be modified/added the system first tries to connect the web server sees if its availible, if not then it stores in locally on the device until the connection comes back, now I noticed that if the device does not mess around with the data connection then the application sits nicely as long as its open, and never gets shut down. ... But again most of the time I get no exception at all but just simply disappears. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Strange sudden shutdown issue with CF app
    ... Phone devices are notoriously low on virtual memory right out of the box because the OEMs decide to put every little thing into its own DLL and then have 25 processes launched at boot. ... Chris Tacke, Embedded MVP ... We have created an app that basically connects to a web service gets data, then stores it locally on the device, and when a record needs to be modified/added the system first tries to connect the web server sees if its availible, if not then it stores in locally on the device until the connection comes back, now I noticed that if the device does not mess around with the data connection then the application sits nicely as long as its open, and never gets shut down. ... But again most of the time I get no exception at all but just simply disappears. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Remobjects, good in multi-threading environment?
    ... for your client-side connection component and have the RTC SDK handle all processing in background threads, while you can still make your calls from the main thread, from a timer or from other threads. ... And any exception causes a very thorough ... Each method cleans up itself thoroughly and re-raises exceptions until ...
    (borland.public.delphi.thirdpartytools.general)