单例模式在JDBC数据库连接操作里的应用


设计模式之单例模式一般应用在在数据库操作里,数据库操作就要常常创建实例,然后进行数据库操作,全部就能够

将数据库操作的方法。进行封装,然后採用单例模式进行设计,然后採用单例模式之后,就能够节约系统资源。对于

一些须要频繁创建和销毁的对象单例模式无疑能够提高系统的性能



先编写数据库配置文件config.properties

host=localhost
port=3306
database=tourism_system
username=root
password=111



DBHelpUtil.java:

/**
 * 
 * 数据库连接的类。配置信息保存在config.properties里
 *
 */
public class DBHelperUtil {
	//静态成员变量。支持单态模式
	private static DBHelperUtil manager = null;
	//配置资源文件
	private PropertyResourceBundle bundle;
	//JDBC驱动
	private static String jdbcDriver = null;
	//主机
	private String host = "";
	//数据库端口
	private String port = "";
	//数据库名称
	private String database = "";
	//数据库用户名
	private String username = "";
	//数据库密码
	private String password ="";
	
	//数据库连接字符串
	private String connStr = "";
	
	//连接对象
	private Connection conn = null;
	//PrepareStatement对象
	private PreparedStatement pstm = null;
	//CallableStatement对象
	private CallableStatement cstm = null;
	
	/**
	 * 私有构造对象,不能够实例化
	 * @throws IOException
	 */
	public DBHelperUtil() throws IOException{
		bundle = new PropertyResourceBundle(DBHelperUtil.class.getResourceAsStream("config.properties"));
		this.host = getString("host");
		this.database = getString("database");
		this.port = getString("port");
		this.username = getString("username");
		this.password = getString("password");
		jdbcDriver = "com.mysql.jdbc.Driver";
		//数据库连接的url。设置了编码为UTF-8
		connStr = "jdbc:mysql://"+host+":"+port+"/"+database+"?

useUnicode=true&characterEncoding=UTF-8"; } /** * 读取配置文件里的值 * @param * key 配置文件的key * @return * key相应的值 */ private String getString(String key){ return this.bundle.getString(key); } /** * 单态模式获取实例 * * @return SqlManager对象 * @throws IOException * @throws ClassNotFoundException */ public static DBHelperUtil createInstance() throws IOException, ClassNotFoundException{ if (manager == null) { manager = new DBHelperUtil(); manager.initDB(); } return manager; } /** * 初始化连接參数,由指定的DBType生成 * * @throws ClassNotFoundException */ public void initDB() throws ClassNotFoundException{ Class.forName(jdbcDriver); } /** * 连接数据库 * @throws SQLException */ public void connectDB() throws SQLException{ conn = DriverManager.getConnection(connStr,username,password); conn.setAutoCommit(false);// 设置自己主动提交为false } /** * 关闭数据库,释放内存 * @throws SQLException */ public void close() throws SQLException { if (pstm != null) { pstm.close(); } if (cstm != null) { cstm.close(); } if (conn != null) { conn.close(); } } /** * 设置PrepareStatement对象中Sql语句中的參数 * @param sql * sql语句 * @param params * 參数列表 * @throws SQLException */ @SuppressWarnings("unused") private void setPrepareStatementParams(String sql, Object[] params) throws SQLException{ pstm = conn.prepareStatement(sql); // 获取对象 if (params != null) { for (int i = 0; i < params.length; i++) // 遍历參数列表填充參数 { pstm.setObject(i + 1, params[i]); } } } /** * 运行查询 * * @param sql * sql语句 * @param params * 參数列表 * @return 返回ResultSet类型的查询结果 * @throws SQLException */ public ResultSet executeQuery(String sql, Object[] params) throws SQLException{ // 运行查询数据库接口 ResultSet rs = null; manager.setPrepareStatementParams(sql, params); // 填充參数 rs = pstm.executeQuery(); // 运行查询操作 return rs; } /** * 更新数据库操作 * * @param sql * sql语句 * @param params * 參数列表 * @return 运行操作的结果 * @throws SQLException */ public boolean executeUpdate(String sql, Object[] params)throws SQLException { // 运行无返回数据的数据查询,返回值是被改变的书库的数据库项数 boolean result = false; manager.setPrepareStatementParams(sql, params); // 填充參数 pstm.executeUpdate(); // 运行更新 manager.commitChange(); result = true; return result; } /** * 提交信息到数据库 * @throws SQLException */ private void commitChange() throws SQLException { conn.commit(); } }


调用工具类:

先创建实例,createInstance。然后连接数据库。调用方法就能够





posted @ 2017-08-17 19:32  jhcelue  阅读(323)  评论(0编辑  收藏  举报