java 数据库连接方式(六)
2011-07-30 22:37
429 查看
import com.xxx.config.ConfigManager; import com.xxx.config.Configs; /** * 连接管理类 * @author */ public class ConnectionManager { //数据库类型 private static int dbType = -1; private static ConnectionFactory _connFactory; /** * oracle数据库类型标志位 */ public static int ORACLE = 1; /** * Mysql数据库类型标志位 */ public static int MYSQL = 2; /** * 得到数据库类型 * @return 数据库类型标志位 */ public static int getDBType() { if (dbType == -1) { String type = ConfigManager.getKey(ConfigKeys.dbTypeKey); if ("oracle".equals(type)) { dbType = ORACLE; } else if ("mysql".equals(type)){ dbType = MYSQL; } else dbType = 0; } return dbType; } /** * 根据指定的数据库配置得到ConnectionFactory对象。 * @return ConnectionFactory对象 */ public static ConnectionFactory getConnectionFactory(Configs conf) { ConnectionFactory fac = null; String type = conf.getConfig(ConfigKeys.dbConnTypeKey); if ("jndi".equals(type)) { fac = new JndiConnectionFactory(); } else { String cls = conf.getConfig(ConfigKeys.dbConnClassKey); if (cls == null || cls.length() == 0) { cls = SimpleConnectionFactory.class.getName(); } ClassLoader cl = ConnectionManager.class.getClassLoader(); try { fac = (ConnectionFactory) cl.loadClass(cls).newInstance(); } catch (Exception ex) { ex.printStackTrace(); fac = new SimpleConnectionFactory(); } } fac.init(conf); return fac; } /** * 根据默认的数据库配置得到ConnectionFactory对象,数据库操作的入口。默认实现类SimpleConnectionFactory * @return ConnectionFactory对象 */ public synchronized static ConnectionFactory getConnectionFactory() { if (_connFactory == null) { Configs conf = ConfigManager.getDefaultConfigs(); _connFactory = getConnectionFactory(conf); } return _connFactory; } static Class connectionClass; static ConnectionObject newConnectionObject() { if (connectionClass == null) { String cls = ConfigManager.getDefaultConfigs().getConfig("ConnectionObjectClassName"); if (cls != null && cls.length() > 0) { ClassLoader cl = ConnectionManager.class.getClassLoader(); try { connectionClass = cl.loadClass(cls); } catch (Exception ex) { } } if (connectionClass == null) { connectionClass = ConnectionObject.class; return new ConnectionObject(); } } try { return (ConnectionObject) connectionClass.newInstance(); }catch(Exception ex){} return new ConnectionObject(); } }
相关文章推荐
- JAVA连接数据库的方式
- java连接数据库的几种方式
- 请教评价java连接数据库的几种不方式
- java application 应用程序 使用JDBC proxool 两种方式连接数据库 测试程序代码
- #.Java使用JDBC连接数据库的三种方式
- JAVA连接数据库的方式有多种
- web连接数据库时,报空指针java.lang.NullPointerException问题--可能的解决方式
- Java连接Redis数据库 事务、管道和分布式的调用方式
- java连接各种数据库方式速查表
- java使用jdbc方式连接数据库实例
- 在java 最简单连接数据库的方式
- Java连接数据库的标准方式
- Java连接各种数据库的jdbc连接方式
- java使用url和tns两种方式连接数据库执行存储过程
- 各种数据库的jdbc驱动下载及连接方式-转自javaeye.com author:benlsoft
- java 数据库连接方式(五)
- java jdbc连接数据库的 3 种方式
- java 连接数据库的方式
- JAVA 连接常见数据库的方式总结
- java 数据库连接方式(七)