Obtaining a connection through JNDI

How to obtain a connection through JNDI. Here’s a quick snippet:


public static Connection getJNDIConnection(String dataSourceName) throws NamingException, SQLException {

    Context envctx = (Context)new InitialContext().lookup("java:comp/env");
    DataSource datasource = (DataSource) envctx.lookup(dataSourceName);
    Connection conn = datasource.getConnection();
    return conn;


}

.