package db; import java.sql.Connection; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import java.sql.PreparedStatement; import java.sql.Statement; import java.sql.ResultSet; public class Conn { private static Conn instance = new Conn(); public static Conn getInstance() { return instance; } private Conn() { } public Connection getConnection() throws SQLException { Context initContext = null; try { initContext = new InitialContext(); // Context envContext = (Context) initContext.lookup("java:comp/env"); // DataSource ds = (DataSource) envContext.lookup("jdbc/oracleDS"); DataSource ds = (javax.sql.DataSource) initContext.lookup ("oracleDS"); return ds.getConnection(); } catch(NamingException e) { System.out.println("DataSource Err :" + e.toString()); } return null; } public void freeConnection(Connection c, PreparedStatement p, ResultSet r) throws SQLException { try{ if(r!=null) r.close(); if(p!=null) p.close(); if(c!=null) c.close(); } catch(SQLException e){ e.printStackTrace(); } } public void freeConnection(Connection c, Statement s, ResultSet r) throws SQLException { try{ if(r!=null) r.close(); if(s!=null) s.close(); if(c!=null) c.close(); } catch(SQLException e){ e.printStackTrace(); } } public void freeConnection(Connection c, PreparedStatement p) throws SQLException { try{ if(p!=null) p.close(); if(c!=null) c.close(); } catch(SQLException e){ e.printStackTrace(); } } public void freeConnection(Connection c, Statement s) throws SQLException { try{ if(s!=null) s.close(); if(c!=null) c.close(); } catch(SQLException e){ e.printStackTrace(); } } }