由于公司的框架体系太成熟,所以工作一段时间后导致最底层的东西几乎忘掉,今天趁闲暇时间又把jdbc基础捡了一下并写了一个较为通用的工具类,其中包括事务模板,简单查询,修改,有不足的地方忘大牛们多提建议,拍转,工具类主要包括DaoSupport、DaoSupportTemplate、DaoSupportAction。
1、DaoSupport 工具类,封装了自己实现的连接池和jdbc简单的查询和修改。
/**
* Alipay.com Inc.
* Copyright (c) 2004-2013 All Rights Reserved.
*/
package com.jinfeng.practice.util;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import com.jinfeng.practice.util.impl.DaoSupportTemplateImpl;
/**
* dao支持类
*
* @author feng.jin
* @version $Id: DaoSupport.java, v 0.1 2013-8-7 上午10:58:28 feng.jin Exp $
*/
public class DaoSupport {
??? /** 数据连接池 */
??? private static final List<Connection> connectionPool??? = new LinkedList<Connection>();
??? /** 驱动 */
??? private static String???????????????? driver;
??? /** url*/
??? private static String???????????????? url;
??? /** username */
??? private static String???????????????? username;
??? /** password */
??? private static String???????????????? password;
??? /** properties */
??? private static final Properties?????? properties??????? = new Properties();
??? /** 最大连接数 */
??? private static final int????????????? MAX?????????????? = 10;
??? /** 活跃连接数 */
??? private static int??????????????????? activeNum???????? = 10;
??? /** 已用连接数 */
??? private static int??????????????????? usedNum?????????? = 0;
??? /** 连接 */
??? private Connection??????????????????? conn????????????? = null;
??? /** 语句平台 */
??? private PreparedStatement???????????? preparedStatement = null;
??? /** 模板 */
??? private final DaoSupportTemplate????? daoSupportTemplate;
??? /**
???? * constructor
???? */
??? public DaoSupport() {
??????? daoSupportTemplate = new DaoSupportTemplateImpl();
??????? daoSupportTemplate.setDaoSupport(this);
??? }
??? static {
??????? try {
??????????? //1.【初始化数据连接需要的数据】
??????????? properties.load(DaoSupport.class.getResourceAsStream("/config/datasource.properties"));
??????????? driver = properties.getProperty("jdbc.driver");
??????????? url = properties.getProperty("jdbc.url");
??????????? username = properties.getProperty("jdbc.username");
??????????? password = properties.getProperty("jdbc.password");
??????????? Class.forName(driver);
??????????? //2.【初始化连接池】
??????????? for (int i = 0; i < 10; i++) {
??????????????? Connection conn = DriverManager.getConnection(url, username, password);
??????????????? connectionPool.add(conn);
??????????? }
??????? } catch (IOException e) {
??????????? System.out.println(e);
??????? } catch (ClassNotFoundException e) {
??????????? System.out.println(e);
??????? } catch (SQLException e) {
??????????? System.out.println(e);
??????? }
??? }
??? /**
???? * 获取连接
???? *
???? * @return
???? */
??? public synchronized Connection getConnection() {
??????? if (conn == null) {
??????????? if (connectionPool != null && connectionPool.size() >= 0) {
??????????????? conn = connectionPool.get(0);
??????????????? connectionPool.remove(0);
??????????????? activeNum--;
??????????????? usedNum++;
??????????? } else {
??????????????? throw new RuntimeException("当前使用的连接数已经超过" + MAX + ", 连接池没有足够的数据库连接!");
??????????? }
??????? } else {
??????????? return conn;
??????? }
??????? return conn;
??? }
??? /**
???? * 获取语句执行器
???? *
???? * @para