package com.cxsoft.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ResourceBundle;


/**
 * 数据库链接类
 * 适用于oracle,db2,mysql
 * 通过构造函数传入值区别是那种数据库
 * @author 数据库链接基础类
 *
 */
public class DBConnection {
	public static final String ORACLE_FLAG = "oracle" ; 
	public static final String DB2_FLAG = "db2" ; 
	public static final String MYSQL_FLAG = "mysql" ; 
	/**  数据库连接URL */
	private String DBurl;

	/**  数据库连接驱动 */
	private String DBdriver;
	
	/** 数据库用户名 */
	private String DBuser;

	/** 数据库密码 */
	private String DBpwd;
	

	/**
	 * @param dbType :数据库类型 如:oracle,mysql
	 */
	public DBConnection(DBModel model){
		this.initDBConfig(model);
	}
	
	/**
	 * 初始化数据库值
	 * @param dbType: 数据库类型 如:oracle,mysql
	 */
	private void initDBConfig(DBModel model) {
		try {
			ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
			if(ORACLE_FLAG.equals(model.getDBtype())){
				DBdriver = bundle.getString("db.oracle.driver");
				DBurl =(checkIsNull(model.getDBurl()))? bundle.getString("db.oracle.url"):model.getDBurl();
//				DBurl = bundle.getString("db.oracle.url");
				DBuser = (checkIsNull(model.getDBname()))?bundle.getString("db.oracle.username"):model.getDBname();
				DBpwd = (checkIsNull(model.getDBpwd()))?bundle.getString("db.oracle.password"):model.getDBpwd(); 
			}
			if(DB2_FLAG.equals(model.getDBtype())){
				DBdriver = bundle.getString("db.db2.driver");
				DBurl =(checkIsNull(model.getDBurl()))? bundle.getString("db.db2.url"):model.getDBurl();
				DBuser = (checkIsNull(model.getDBname()))?bundle.getString("db.db2.username"):model.getDBname();
				DBpwd = (checkIsNull(model.getDBpwd()))?bundle.getString("db.db2.password"):model.getDBpwd(); 
//				DBuser = bundle.getString("db.db2.username");
//				DBpwd = bundle.getString("db.db2.password");
			}
			if(MYSQL_FLAG.equals(model.getDBtype())){
				DBdriver = bundle.getString("db.mysql.driver");
//				DBurl = bundle.getString("db.mysql.url");
				DBurl =(checkIsNull(model.getDBurl()))? bundle.getString("db.mysql.url"):model.getDBurl();
				DBuser = (checkIsNull(model.getDBname()))?bundle.getString("db.mysql.username"):model.getDBname();
				DBpwd = (checkIsNull(model.getDBpwd()))?bundle.getString("db.mysql.password"):model.getDBpwd(); 
//				DBuser = bundle.getString("db.mysql.username");
//				DBpwd = bundle.getString("db.mysql.password");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	/**
	 * 判断字符串是否为空
	 * @param str
	 * @return
	 */
	private boolean checkIsNull(String str){
		boolean flag = false ;
		if("".equals(str) || str == null){
			flag = true;
		}
		return flag ;
	}
	/**
	 * 获取数据库连接
	 * 
	 * @return
	 */
	public Connection getConnection() {
		/** 声明Connection连接对象 */
		Connection conn = null;
		try {
			/** 使用Class.forName()方法自动创建这个驱动程序的实例且自动调用DriverManager来注册它 */
			Class.forName(DBdriver);
			/** 通过DriverManager的getConnection()方法获取数据库连接 */
			conn = (Connection) DriverManager.getConnection(DBurl,DBuser, DBpwd);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return conn;
	}

	/**
	 * 关闭数据库连接
	 * 
	 * @param connect
	 */
	public void closeConnection(Connection conn) {
		try {
			if (conn != null) {
				/** 判断当前连接连接对象如果没有被关闭就调用关闭方法 */
				if (!conn.isClosed()) {
					conn.close();
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("关闭数据库连接异常:"+ex);
		}
	}
	
	public static void main(String[] args) {
//		new DBConnection(MYSQL_FLAG).getConnection();
	}
}
最近下载更多
yjdang  LV3 2023年3月27日
1214066599  LV8 2022年4月4日
1306878374  LV13 2021年12月21日
Tayirjan  LV11 2021年7月18日
qewqqeqw  LV2 2020年8月18日
xp95323  LV14 2019年12月1日
xuyongff  LV24 2019年11月26日
906396135  LV7 2019年10月31日
cralgogo  LV2 2019年10月28日
0312wangchen  LV26 2019年9月18日
最近浏览更多
taoshen95  LV14 2023年7月3日
lyz199274  LV1 2023年4月21日
yjdang  LV3 2023年3月27日
oonesun 2023年1月31日
暂无贡献等级
danta123459  LV6 2022年7月29日
cxdxfx12  LV14 2022年6月18日
1214066599  LV8 2022年4月4日
zmy10086  LV4 2022年2月5日
jy1218  LV12 2022年1月18日
空心菜4  LV9 2022年1月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友