Monday, October 22, 2012

How to Get the Connection using Data Source?

How to Get the Connection using Data Source?

    public static Connection getConnection() {
        Connection con = null;
        try {
           
            String dsURL = "jdbc/TestDS";
            Context initialContext = new InitialContext();
            if (initialContext == null) {
            
            }
            DataSource datasource = (DataSource)initialContext.lookup(dsURL);
            if (datasource != null) {
                con = datasource.getConnection();
                con.setAutoCommit(false);
            } else {
          
            }
        } catch (NamingException ex) {
         
            ex.printStackTrace();
        } catch (SQLException e) {
   
        } catch (Exception e) {
   
        }
        return con;
    }