Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.Properties;
- import java.io.InputStream;
- import javax.naming.InitialContext;
- import javax.sql.DataSource;
- import org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory;
- private static String JDBC_DRIVER = "jdbc.driver";
- private static String JDBC_URL = "jdbc.url";
- private static String JDBC_USER = "jdbc.user";
- private static String JDBC_PASSWORD = "jdbc.password";
- private static Properties props = new Properties();
- private Connection connection = null;
- private Statement stat = null;
- private ResultSet rs = null;
- private static volatile DataSource dsObj;
- static {
- try {
- // a way to retrieve the data in
- // connection.properties found
- // in WEB-INF/classes
- InputStream is = DbConnector.class.getResourceAsStream("/connection.properties");
- props.load(is);
- //PropertyConfigurator.configure("log4j.properties");
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- Class.forName(props.getProperty(JDBC_DRIVER)).newInstance();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static void initialize() {
- try {
- dsObj = BasicDataSourceFactory.createDataSource(props);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Constructor
- */
- public DbConnector() {
- try {
- initialize();
- this.connection = getConnection();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Returns DB Connection
- * @return Connection
- * @throws SQLException
- */
- public static Connection getConnectionFromPool() throws SQLException {
- Connection connection = null;
- // checking for null singleton instance
- if (null == dsObj) { // synchronized over class to make thread safe
- synchronized (DbConnector.class) {
- // double checking for making singleton instance thread safe
- if (null == dsObj) {
- initialize();
- }
- }
- }
- // getting connection from data sourceconnection = dsObj.getConnection();
- return connection;
- }
- /**
- * Get Connection
- * @return Connection object
- * @throws SQLException
- */
- private Connection getConnection() throws SQLException {
- return DriverManager.getConnection(props.getProperty(JDBC_URL), props.getProperty(JDBC_USER), props.getProperty(JDBC_PASSWORD));
- }
- /**
- * Execute Query
- * Purpose: SELECT
- * @param sql SQL Statement
- * @return ResultSet
- */
- public ResultSet executeQuery(String sql) {
- try {
- if (connection == null) {
- return null;
- }
- stat = connection.createStatement();
- rs = stat.executeQuery(sql);
- return rs;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * Execute Update
- * Purpose: Insert, Update, Delete
- * @param sql SQL Statement
- * @return int No. of Rows Updated
- */
- public int executeUpdate(String sql) {
- try {
- if (connection == null) {
- return -1;
- }
- stat = connection.createStatement();
- return stat.executeUpdate(sql);
- } catch (Exception e) {
- //e.printStackTrace();
- return -1;
- }
- }
- /**
- * Execute
- * Purpose: Create, Drop
- * @param sql statement to update.
- * @return true is statement execute sucessfuly and false otherwise
- */
- public boolean execute(String sql) {
- try {
- if (connection == null) {
- return false;
- }
- stat = connection.createStatement();
- return stat.execute(sql);
- } catch (Exception e) {
- //e.printStackTrace();
- return false;
- }
- }
- /**
- * Close ResultSet
- */
- public void closeResultSet() {
- if (rs != null) {
- try {
- rs.close();
- } catch (Exception e) {
- //e.printStackTrace();
- }
- }
- }
- /**
- * Close Statement
- */
- public void closeStatement() {
- if (stat != null) {
- try {
- stat.close();
- } catch (Exception e) {
- e.printStackTrace();
- //log.error(e);
- }
- }
- }
- /**
- * Close Connection
- */
- public void closeConnection() {
- try {
- connection.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Close
- * Connection, Statement and Resultset *
- */
- public void close() {
- try {
- if (rs != null) {
- rs.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- try {
- if (stat != null) {
- stat.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- try {
- if (connection != null) {
- connection.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- <resource-ref>
- <description>Customer Database</description>
- <res-ref-name>jdbc/CustomerDB</res-ref-name>
- <res-type>javax.sql.DataSource</res-type>
- <res-auth>Container</res-auth>
- </resource-ref>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement