Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sm.data;
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.Wrapper;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import javax.sql.DataSource;
- public class DBPool {
- static Connection connection = null;
- static {
- try {
- Context context = new InitialContext();
- if (context == null) {
- throw new Exception("Uh oh -- no context!");
- }
- DataSource ds = (DataSource) context.lookup("java:/comp/env/jdbc/PineappleDB");
- if (ds == null) {
- throw new Exception("Data Source not found!");
- }
- connection = ds.getConnection();
- } catch (NamingException | SQLException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static Connection getConnection() {
- return connection;
- }
- public static Connection validConnection(Connection conn) throws SQLException {
- if (conn == null || conn.isClosed()) {
- conn = DBPool.getConnection();
- }
- return conn;
- }
- public static void slaughter(Wrapper... obj) {
- for (Wrapper o : obj) {
- try {
- if (o != null) {
- if (o instanceof Connection) {
- ((Connection) o).close();
- } else if (o instanceof CallableStatement) {
- ((CallableStatement) o).close();
- } else if (o instanceof Statement) {
- ((Statement) o).close();
- } else if (o instanceof ResultSet) {
- ((ResultSet) o).close();
- }
- }
- } catch (SQLException e) {
- } finally {
- o = null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement