All pastes #2104470 Raw Edit

DB.java

public java v1 · immutable
#2104470 ·published 2012-01-20 13:00 UTC
rendered paste body
/** *  */package db.sqlserver;import java.lang.reflect.Method;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.sql.DataSource;import play.Logger;import play.exceptions.DatabaseException;/** * @author eknowit * */public class DB {	public static DataSource datasource = null;	static ThreadLocal<Connection> localConnection = new ThreadLocal<Connection>();	static String destroyMethod = "";	public static void close() {		if (localConnection.get() != null) {			try {				Connection connection = localConnection.get();				localConnection.set(null);				connection.close();			} catch (Exception e) {				throw new DatabaseException("It's possible than the connection was not properly closed !", e);			}		}	}	public static Connection getConnection() {		try {			if (localConnection.get() != null) {				return localConnection.get();			}			Connection connection = datasource.getConnection();			localConnection.set(connection);			return connection;		} catch (SQLException ex) {			throw new DatabaseException("Cannot obtain a new connection (" + ex.getMessage() + ")", ex);		} catch (NullPointerException e) {			if (datasource == null) {				throw new DatabaseException("No database found. Check the configuration of your application.", e);			}			throw e;		}	}	public static void destroy() {		try {			if (DB.datasource != null && DB.destroyMethod != null && !DB.destroyMethod.equals("")) {				Method close = DB.datasource.getClass().getMethod(DB.destroyMethod, new Class[] {});				if (close != null) {					close.invoke(DB.datasource, new Object[] {});					DB.datasource = null;					Logger.trace("Datasource destroyed");				}			}		} catch (Throwable t) {			Logger.error("Couldn't destroy the datasource", t);		}	}	public static List<Map<String, Object>> executeQuery(String sql, Object... params) {		Connection con = null;		PreparedStatement pstm = null;		ResultSet rs = null;		try {			con = DB.getConnection();			pstm = con.prepareStatement(sql);			int i;			for (i = 0; i < params.length; i++) {				pstm.setObject(i + 1, params[i]);			}			rs = pstm.executeQuery();			List<Map<String, Object>> resp = new ArrayList<Map<String, Object>>();			ResultSetMetaData metadata = rs.getMetaData();			int columns = metadata.getColumnCount();			Map<String, Object> element;			while (rs.next()) {				element = new HashMap<String, Object>();				for (i = 1; i <= columns; i++) {					element.put(metadata.getColumnLabel(i), rs.getObject(i));				}				resp.add(element);			}			return resp;		} catch (SQLException ex) {			throw new DatabaseException(ex.getMessage(), ex);		} finally {			try {				if (rs != null) {					rs.close();				}				if (pstm != null) {					pstm.close();				}				if (con != null) {					DB.close();				}			} catch (Exception e) {				System.out.println("HOO: No manejado !!!");			}		}	}	}