Jdbc 数据库连接池简易实现和JdbcUtils
public class JdbcUtils {/*private static String url = "jdbc:mysql://localhost:3306/riverevaluationsys";private static String user = "root";private static String password = "root";*/private static MyDataSource myDataSource ;// 装入虚拟机,立即执行static {try {Class.forName("com.mysql.jdbc.Driver");myDataSource = new MyDataSource();} catch (ClassNotFoundException e) {throw new ExceptionInInitializerError(e);}}/** * * getConnection:<br /> * 获取数据库的连接 * * @author zhangzhaoyu * @return * @throws SQLException */public static Connection getConnection() throws SQLException {//return DriverManager.getConnection(url, user, password);return myDataSource.getConnection();}/** * * close:<br /> * 关闭资源 * * @author zhangzhaoyu * @param conn * @param rs * @param st */public static void close(Connection conn, ResultSet rs, Statement st) { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (st != null) st.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { myDataSource.free(conn); } catch (Exception e) { e.printStackTrace(); } } } }/** * * close:<br /> * 关闭数据库重载函数 * * @author zhangzhaoyu * @param conn * @param rs * @param pst */public static void close(Connection conn, ResultSet rs, PreparedStatement pst) { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (pst != null) pst.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) try { myDataSource.free(conn); } catch (Exception e) { e.printStackTrace(); } } } }/** * * close:<br /> * 关闭资源 * * @author zhangzhaoyu * @param conn * @param pst */public static void close(Connection conn, PreparedStatement pst) { try { if (pst != null) pst.close(); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) try { myDataSource.free(conn); } catch (Exception e) { e.printStackTrace(); } } } }