Am Mittwoch, den 08.11.2006, 02:07 +0100 schrieb Markus Schönhaber:
> Damit man tatsächlich mit der Datenbank arbeiten kann, benötigt es ja noch 
> eine java.sql.Connection. In Deinem Beispiel (<res-auth>Container</res-auth>, 
> Host, DB, User, Paßwort im Container konfiguriert) würde also noch etwas wie
> java.sql.Connection conn = ds.getConnection();
> gebraucht.
Ja, hier ist der Code Komplett, er hat zwar klein wenig mehr als was wir
beispielen wollen aber er ist echt :
    public static final String CAS_INT_DSN="jdbc/cas_int";
    public static final String AUTH_DSN="jdbc/hs_access";
    private static Connection conAuth=null;
    private static Connection conCasInt=null;
    /**
     *
     [EMAIL PROTECTED] dsn DataSourceName
     [EMAIL PROTECTED] java.sql.Connection
     */
    synchronized public static Connection getConnection(String dsn)
throws SQLException{
        InitialContext ctx=null;
        DataSource ds=null;
        
        try{
            ctx = new InitialContext();
            ds = (DataSource) ctx.lookup(dsn);
        } catch(NamingException nw) {
            try{
                ds = (DataSource) ctx.lookup("java:comp/env/"+dsn);
            } catch (NamingException e){
                e.printStackTrace();
            }
        }
        
        if(dsn.equals(AUTH_DSN)) {
            if(conAuth==null)
                conAuth=ds.getConnection();
            return conAuth;
        } else if(dsn.equals(CAS_INT_DSN)) {
            if(conCasInt==null)
                conCasInt=ds.getConnection();
            return conCasInt;
        } else {
            return ds.getConnection();
        }
    }


-- 
----------------------------------------------------------------------------
PUG - Penguin User Group Wiesbaden - http://www.pug.org

Antwort per Email an