I wish to use A DataTap to select a Database name and detail from a Properties file.I want to use a Business Object, DAO, Connection Pool and POJO.
I have a Data Tap,BO template,DAO Template,Connection Pool example(From DAO example on this site "I wish to add transaction support.").
I need some guidance to how to link these together.
This is a stand alone
Java Application(J2SE) to a Access Database.The POJO has a Heiracical structure.Her is some code:
BUSINESS OBJECT
public class UserBO { private static ResourceBundle db_Identity =ResourceBundle.getBundle("dBUsage");public Final DbType = db_Identity.getString("db"); private ConnectionPool pool; public UserBO() {//Call Method to load to type of DB DataTap con = DataTapFactory.getDataSourceAccessor (dbType); }
DATATAP
public class DataTapFactory { public Final accessUrl = accessDB_Properties.getString("url");public Final accessUsername = accessDB_Properties.getString("username");public Final accessPassword = accessDB_Properties.getString("password");public Final accessDriverName = accessDB_Properties.getString("driverName"); private static ResourceBundle accessDB_Properties =ResourceBundle.getBundle("accessDBProperties"); public static DataTap getDataSourceAccessor(
String dbType) { if (dbType.equals("ACCESS")) { //connection ConnectionPool connPool = new ConnectionPool(accessUrl, accessUsername, accessPassword, accessDriverName); return conn; } return null; }}
CONNECTION POOL
Note:I don`t know whether line
this.(Connection)setAutoCommit(autoCommit);
Will work.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; public class ConnectionPool{ private ArrayList _freePool; private String _url; private String _username; private String _password; private String _dbDriverName; public ConnectionPool(){ _freePool = new ArrayList(); } public ConnectionPool(String connectURL, String uName, String pWord, String driverName){ this(); _url = connectURL; _username = uName; _password = pWord; _dbDriverName = driverName; _createConnections(_url,_username,_password,_dbDriverName); } public synchronized Connection getConnection(boolean autoCommit){ if(_freePool.size() == 0){ //None Available //Create a new set of Connections _createConnections(_url,_username,_password,_dbDriverName); } //Last connection in the Array so that we save time on re-arranging the Array this.(Connection)setAutoCommit(autoCommit); return ((Connection)_freePool.remove(_freePool.size() - 1)); } public void closeConnection(Connection con){ System.out.println("Current size of Pool: "+_freePool.size()); _freePool.add(con); System.out.println("Size of Pool after connection closed: "+ _freePool.size()); } private void _createConnections (String connectURL, String uName, String pWord, String driverName){ try{ Class.forName(driverName).newInstance(); for(int i=0; i<10; ++i){ Connection con = DriverManager.getConnection (connectURL,uName,pWord); _freePool.add(con); } }catch(ClassNotFoundException ex){ System.out.println(ex); }catch(InstantiationException ex){ System.out.println(ex); }catch(IllegalAccessException ex){ System.out.println(ex); }catch(SQLException ex){ System.out.println(ex); } } }