[Batchserver-cvs] batchserver/src/org/jmonks/batchserver/framework/util JdbcConnectionHelper.java,
Brought to you by:
suresh_pragada
From: Suresh <sur...@us...> - 2006-09-10 17:57:40
|
Update of /cvsroot/batchserver/batchserver/src/org/jmonks/batchserver/framework/util In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv10082 Added Files: JdbcConnectionHelper.java Log Message: no message --- NEW FILE: JdbcConnectionHelper.java --- /* * JdbcConnectionHelper.java * * Created on September 9, 2006, 10:57 AM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package org.jmonks.batchserver.framework.util; import java.sql.Connection; import java.sql.DriverManager; import java.util.Map; import org.apache.log4j.Logger; /** * * @author Suresh Pragada */ public final class JdbcConnectionHelper { /** * Constant defines the property name for jdbc driver class name. The name * should be "jdbc-driver-class-name". */ public static final String PROPERTY_JDBC_DRIVER_CLASS_NAME = "jdbc-driver-class-name"; /** * Constant defines the property name for database URL. The name * should be "jdbc-url". */ public static final String PROPERTY_JDBC_URL = "jdbc-url"; /** * Constant defines the property name for database user name. The name * should be "username". */ public static final String PROPERTY_DATABASE_USER_NAME = "username"; /** * Constant defines the property name for database password. The name * should be "password". */ public static final String PROPERTY_DATABASE_PASSWORD = "password"; private static Logger logger=Logger.getLogger(JdbcConnectionHelper.class); /** * To restrict the instantiation of JdbcConnectionHelper */ private JdbcConnectionHelper() { } /** * Get the required properties from the given configMap and * establishes the connection. Returns null, if it cannot establish the * connection. * * @param configMap Map contains the properties required to create the connection. * * @return Returns the database connction, null, if it cannot establish the connection. */ public static synchronized Connection getConnection(Map configMap) { logger.trace("Entering getConnection"); logger.info("Connection configuration : " + configMap); Connection connection=null; String driverClassName=(String)configMap.get(JdbcConnectionHelper.PROPERTY_JDBC_DRIVER_CLASS_NAME); String databaseURL=(String)configMap.get(JdbcConnectionHelper.PROPERTY_JDBC_URL); String userName=(String)configMap.get(JdbcConnectionHelper.PROPERTY_DATABASE_USER_NAME); String password=(String)configMap.get(JdbcConnectionHelper.PROPERTY_DATABASE_PASSWORD); if(driverClassName==null || "".equals(driverClassName) || databaseURL==null || "".equals(databaseURL) || userName==null || "".equals(userName) || password==null || "".equals(password)) connection=null; else { try { Class.forName(driverClassName); connection=DriverManager.getConnection(databaseURL,userName, password); } catch(Exception exception) { /** * No need to worry about the specific exception. In any case need to return null. * So catching the generic one. */ exception.printStackTrace(); logger.error(exception.getMessage(), exception); connection=null; } } logger.trace("Exiting getConnection"); return connection; } /** * Closes the given connection. Doesn't throw any exceptions to the caller. */ public static void closeConnection(Connection connection) { try { if(connection!=null && !connection.isClosed()) connection.close(); } catch(Exception exception) { exception.printStackTrace(); logger.error(exception.getMessage(),exception); } } } |