📄 connectionfactory.java
字号:
package com.passedbylove.database.core;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ResourceBundle;
/** 类名:ConnectionFactory<br>
* 作用:产生数据库连接对象<br>
* 属性:<br>
* 方法:Connection getConnection()<br>
* 作用:返回数据库连接对象<br>
* 参数:无<br>
* 返回:数据库连接对象<br>
* 其它:返回的aConnection不会自动提交JDBC事务<br>
* 创建人:被爱情路过<br>
* 创建日期:2008.11.11<br> */
public abstract class ConnectionFactory {
/** 数据库系统的属性名:
* 有效字符串为下列之一: mysql,mssql */
private static final String propertiesName = "using_which_db_system";
/** 获取Connection对象
*
* @return Connection对象
* @throws Exception SqlException异常 */
public static Connection getConnection() throws Exception {
String dbSystem=null;
Connection aConnection; //申明要返回的aConnection对象
ResourceBundle db = ResourceBundle.getBundle("dbsystem"); //读取配置文件
dbSystem=db.getString(propertiesName);
ResourceBundle rb = ResourceBundle.getBundle(dbSystem); //读取配置文件
Class.forName(rb.getString("db.driver")).newInstance(); //申明JDBC驱动程序名
aConnection = DriverManager.getConnection(rb.getString("db.url"),rb.getString("db.user"),rb.getString("db.pass")); //创建aConnection对象
aConnection.setAutoCommit(false); //设置不自动提交事务
//aConnection.setAutoCommit(true);
return aConnection; //返回aConnection对象
}
/** 当前使用的数据库系统名称
*
* @param echoable 如果启用回显将在控制台输出描述当前数据库系统的信息
* @return dbSystem 数据库系统名称 */
public static String getCurrentDataBaseSystem(boolean echoable) {
String dbSystem=null;
ResourceBundle rb = ResourceBundle.getBundle("dbsystem"); //读取配置文件
dbSystem=rb.getString(propertiesName);
if (echoable)System.out.println("the database system what you using are "+dbSystem);
return dbSystem;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -