Java tutorial
package dgw.mt940.db.util; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import org.apache.commons.dbcp2.BasicDataSource; import dgw.mt940.util.ConfigUtil; // // Here are the dbcp-specific classes. // Note that they are only used in the setupDataSource // method. In normal use, your classes interact // only with the standard JDBC API // // // Here's a simple example of how to use the BasicDataSource. // // // Note that this example is very similar to the PoolingDriver // example. // // To compile this example, you'll want: // * commons-pool-2.3.jar // * commons-dbcp-2.1.jar // in your classpath. // // To run this example, you'll want: // * commons-pool-2.3.jar // * commons-dbcp-2.1.jar // * commons-logging-1.2.jar // in your classpath. // // // Invoke the class using two arguments: // * the connect string for your underlying JDBC driver // * the query you'd like to execute // You'll also want to ensure your underlying JDBC driver // is registered. You can use the "jdbc.drivers" // property to do this. // // For example: // java -Djdbc.drivers=org.h2.Driver \ // -classpath commons-pool2-2.3.jar:commons-dbcp2-2.1.jar:commons-logging-1.2.jar:h2-1.3.152.jar:. \ // BasicDataSourceExample \ // "jdbc:h2:~/test" \ // "SELECT 1" // public class DGWBasicDataSource { final static org.apache.log4j.Logger logger4j = org.apache.log4j.Logger.getLogger(DGWBasicDataSource.class); private static DataSource dataSource = null; public DataSource initDGWBasicDataSource() { // First we set up the BasicDataSource. // Normally this would be handled auto-magically by // an external configuration, but in this example we'll // do it manually. // logger4j.debug("Setting up data source."); String connURL = ConfigUtil.getConfigUtil().getConfig("jdbc.dgw.url"); String driver = ConfigUtil.getConfigUtil().getConfig("jdbc.dgw.driverClassName"); String userName = ConfigUtil.getConfigUtil().getConfig("jdbc.dgw.username"); String password = ConfigUtil.getConfigUtil().getConfig("jdbc.dgw.password"); String removeAbandoned = ConfigUtil.getConfigUtil().getConfig("jdbc.dgw.pool.removeAbandoned"); String initSize = ConfigUtil.getConfigUtil().getConfig("jdbc.dgw.pool.initial.size"); String maxIdle = ConfigUtil.getConfigUtil().getConfig("jdbc.dgw.pool.max.size"); try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } dataSource = setupDataSource(connURL, driver, userName, password, removeAbandoned, initSize, maxIdle); logger4j.debug("Done."); return dataSource; } public DataSource initPMMBasicDataSource() { // First we set up the BasicDataSource. // Normally this would be handled auto-magically by // an external configuration, but in this example we'll // do it manually. // logger4j.debug("Setting up data source."); String connURL = ConfigUtil.getConfigUtil().getConfig("jdbc.pmm.url"); String driver = ConfigUtil.getConfigUtil().getConfig("jdbc.pmm.driverClassName"); String userName = ConfigUtil.getConfigUtil().getConfig("jdbc.pmm.username"); String password = ConfigUtil.getConfigUtil().getConfig("jdbc.pmm.password"); String removeAbandoned = ConfigUtil.getConfigUtil().getConfig("jdbc.pmm.pool.removeAbandoned"); String initSize = ConfigUtil.getConfigUtil().getConfig("jdbc.pmm.pool.initial.size"); String maxIdle = ConfigUtil.getConfigUtil().getConfig("jdbc.pmm.pool.max.size"); try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } dataSource = setupDataSource(connURL, driver, userName, password, removeAbandoned, initSize, maxIdle); logger4j.debug("Done."); return dataSource; } public DataSource initTMBCIBasicDataSource() { // First we set up the BasicDataSource. // Normally this would be handled auto-magically by // an external configuration, but in this example we'll // do it manually. // logger4j.debug("Setting up data source."); String connURL = ConfigUtil.getConfigUtil().getConfig("jdbc.tmbci.url"); String driver = ConfigUtil.getConfigUtil().getConfig("jdbc.tmbci.driverClassName"); String userName = ConfigUtil.getConfigUtil().getConfig("jdbc.tmbci.username"); String password = ConfigUtil.getConfigUtil().getConfig("jdbc.tmbci.password"); String removeAbandoned = ConfigUtil.getConfigUtil().getConfig("jdbc.tmbci.pool.removeAbandoned"); String initSize = ConfigUtil.getConfigUtil().getConfig("jdbc.tmbci.pool.initial.size"); String maxIdle = ConfigUtil.getConfigUtil().getConfig("jdbc.tmbci.pool.max.size"); try { Class.forName(driver); } catch (ClassNotFoundException e) { e.printStackTrace(); } dataSource = setupDataSource(connURL, driver, userName, password, removeAbandoned, initSize, maxIdle); logger4j.debug("Done."); return dataSource; } public static DataSource getDataSource() { if (dataSource == null) { DGWBasicDataSource dgwDataSouce = new DGWBasicDataSource(); dataSource = dgwDataSouce.initDGWBasicDataSource(); } return dataSource; } public static DataSource setupDataSource(String connectURI, String driver, String userName, String password, String removeAbandoned, String initSize, String mxSize) { BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(driver); ds.setUrl(connectURI); ds.setUsername(userName); ds.setPassword(password); ds.setUrl(connectURI); ds.setAbandonedUsageTracking(Boolean.getBoolean(removeAbandoned)); ds.setInitialSize(Integer.parseInt(initSize)); ds.setMaxIdle(Integer.parseInt(mxSize)); return ds; } public static void printDataSourceStats(DataSource ds) { BasicDataSource bds = (BasicDataSource) ds; logger4j.debug("NumActive: " + bds.getNumActive()); logger4j.debug("NumIdle: " + bds.getNumIdle()); } public static void shutdownDataSource(DataSource ds) throws SQLException { BasicDataSource bds = (BasicDataSource) ds; bds.close(); } public DataSource getDataSource(String type) { if ("FEX".equalsIgnoreCase(type)) { dataSource = initDGWBasicDataSource(); } else if ("PMM".equalsIgnoreCase(type)) { dataSource = initPMMBasicDataSource(); } return dataSource; } }