JDBC连接池
DBConnection.java
package com.test; import java.io.IOException;
import java.sql.*; public class DBConnection {
//private DBConnectionPoolManager dbc = null; public DBConnection(){
//dbc = DBConnectionPoolManager.getInstance();
} /*public Connection newConnection() throws SQLException{ return dbc.getConnection();
}*/
// private Connection newConnection() throws SQLException {
// //Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
// conn = DriverManager.getConnection("proxool.originDB");
// if (conn == null) {
// throw new SQLException("Connection failed !");
// }else{
// System.out.print("Connection Success !");
// }
// return conn;
// }
public static Connection getMySQLConnection() { Connection conn = null;
try {
conn = DriverManager.getConnection("proxool.dm");
} catch (Exception e) {
System.out.println("Connection failed ! " + e.getMessage());
}
if (conn == null) {
try {
throw new SQLException("Connection failed !");
} catch (SQLException e) {
e.printStackTrace();
}
}else{
System.out.println("Connection Success !");
}
return conn;
} public static Connection getConnection() { Connection conn = null;
try {
conn = DriverManager.getConnection("proxool.mysql");
} catch (Exception e) {
// e.printStackTrace();
System.out.println("Connection failed ! " + e.getMessage());
}
if (conn == null) {
try {
throw new SQLException("Connection failed !");
} catch (SQLException e) {
e.printStackTrace();
}
}else{
System.out.print("Connection Success !");
}
return conn;
}
public static Connection getNewConnection() { Connection conn = null;
try {
//Run as Java Application鐨勬椂鍊欓?氳繃杩欑鏂瑰紡寤虹珛杩炴帴
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","123456");
} catch (Exception e) {
//System.out.println("Connection failed ! " + e.getMessage());
}
if (conn == null) {
try {
throw new SQLException("Connection failed !");
} catch (SQLException e) {
e.printStackTrace();
}
}else{
// System.out.print("Connection Success !");
}
return conn;
} public static void close(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null)
try {
rs.close();
} catch (java.sql.SQLException ex) {
ex.printStackTrace();
}
if (stmt != null)
try {
stmt.close();
} catch (java.sql.SQLException ex) {
ex.printStackTrace();
}
if (conn != null)
try {
conn.close();
} catch (java.sql.SQLException ex) {
ex.printStackTrace();
}
} public static void close(ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} public static void close(Statement st) {
if(st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} public static void close(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} public static void close(ResultSet rs, Statement st) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} public static void close(Statement st, Connection conn) {
if(st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
DBConnection db = new DBConnection();
//DBconn.setConfigFile("waterDB.properties");
// System.out.println(db.getPath());
System.out.println(DBConnection.getNewConnection()); } }
proxool.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- the proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. -->
<something-else-entirely>
<proxool>
<alias>mysql</alias>
<driver-url>jdbc:mysql://127.0.0.1:3306/test</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root" />
<property name="password" value="123456" />
</driver-properties>
<maximum-connection-count>1000</maximum-connection-count>
<minimum-connection-count>2</minimum-connection-count>
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
</proxool>
</something-else-entirely>
web.xml
<servlet>
<servlet-name>ServletConfigurator</servlet-name>
<servlet-class>
org.logicalcobwebs.proxool.configuration.ServletConfigurator
</servlet-class>
<init-param>
<param-name>xmlFile</param-name>
<param-value>WEB-INF/classes/proxool.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
JSP(懒得写Servlet测试了,直接在JSP中测试)
<%@page import="com.test.User"%>
<%@page import="java.sql.*"%>
<%@page import="com.test.DBConnection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
Hello world!
<%
out.println("123");
User user;
//DBConnection //DBConnection dbConnection = new DBConnection();
//User user = new User();
Connection connection = DBConnection.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from t_user");
while(resultSet.next()){
out.print("<br/>");
out.print(resultSet.getInt(1) + " ");
out.print(resultSet.getString(2) + " ");
out.print(resultSet.getString(3));
}
DBConnection.close(resultSet);
DBConnection.close(statement);
DBConnection.close(connection); %>
</body>
</html>
运行结果perfect
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1537190
JDBC连接池的更多相关文章
- 四大流行的jdbc连接池之C3P0篇
C3P0是一个开放源代码的JDBC连接池,它在lib目录中与Hibernate一起发布,包括了实现jdbc3和jdbc2扩展规范说明的Connection 和Statement 池的DataSourc ...
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(九)数据层优化-jdbc连接池简述、druid简介
日常啰嗦 终于回到既定轨道上了,这一篇讲讲数据库连接池的相关知识,线程池以后有机会再结合项目单独写篇文章(自己给自己挖坑,不知道什么时候能填上),从这一篇文章开始到本阶段结束的文章都会围绕数据库和da ...
- JDBC连接池-C池3P0连接
JDBC连接池-C3P0连接 c3p0连接池的学习英语好的看英文原版 c3p0 - JDBC3 Connection and Statement Pooling 使用c3p0连接池 三种方 ...
- JDBC连接池(三)DBCP连接池
JDBC连接池(三)DBCP连接池 在前面的随笔中提到 了 1.JDBC自定义连接池 2. C3P0连接池 今天将介绍DBCP连接池 第一步要导入jar包 (注意:mysql和mysql 驱动 ...
- JDBC连接池-自定义连接池
JDBC连接池 java JDBC连接中用到Connection 在每次对数据进行增删查改 都要 开启 .关闭 ,在实例开发项目中 ,浪费了很大的资源 ,以下是之前连接JDBC的案例 pack ...
- Jmeter(九)JDBC连接池
JDBC为java访问数据库提供通用的API,可以为多种关系数据库提供统一访问.因为SQL是关系式数据库管理系统的标准语言,只要我们遵循SQL规范,那么我们写的代码既可以访问MySQL又可以访问SQL ...
- jdbc连接池&改进dbUtil成C3P0Util
一.jdbc连接池 1.连接池的存在理由 前面介绍的dbUtils工具类虽然实现了一个对jdbc的简单封装.但它依旧采取从驱动管理获取连接 (DriverManager.getConnection ...
- Tomcat 的 JDBC 连接池
JDBC 连接池 org.apache.tomcat.jdbc.pool 是 Apache Commons DBCP 连接池的一种替换或备选方案. 那究竟为何需要一个新的连接池? 原因如下: Comm ...
- 【JDBC&Dbutils】JDBC&JDBC连接池&DBUtils使用方法(重要)
-----------------------JDBC---------- 0. db.properties文件 driver=com.mysql.jdbc.Driver url=jdbc: ...
- spring boot(10)-tomcat jdbc连接池
默认连接池 tomcat jdbc是从tomcat7开始推出的一个连接池,相比老的dbcp连接池要优秀很多.spring boot将tomcat jdbc作为默认的连接池,只要在pom.xml中引入了 ...
随机推荐
- 【poj1987】 Distance Statistics
http://poj.org/problem?id=1987 (题目链接) 题意 给出一棵树,求树上距离不超过K的点对个数. Solution 点分治,同poj1741. 代码 // poj1987 ...
- Restful api介绍
网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致AP ...
- FastCopy包含和排除文件夹处理
包含和排除文件夹操作: 1.有多个时,用[;]进行分割. 2.可指定文件夹深度,也可以不用指定,直接最终名称. 3.不用指定盘符. 4.名称后面带上反斜杠[\]. 假如有两个文件夹:F:\A,F:\B ...
- 深入学习 memset 函数
最近,和同学讨论了一下memset函数,趁着周五空闲做一总结. memset函数最常用的功能就是初始化数组了(主要是置零),如 #include <iostream> #include & ...
- 常用sql语句整理[SQL Server]
1. 存储过程 CREATE PROCEDURE [dbo].[bbs_move_createtopic] @fid smallint, @iconid smallint, @curtid INT O ...
- yield和python(如何生成斐波那契數列)
您可能听说过,带有 yield 的函数在 Python 中被称之为 generator(生成器),何谓 generator ? 我们先抛开 generator,以一个常见的编程题目来展示 yield ...
- HDU 5908 Abelian Period(暴力+想法题)
传送门 Description Let S be a number string, and occ(S,x) means the times that number x occurs in S. i. ...
- C++ 第二次课堂作业(反转链表)
题目链接: 传送门 GitHub链接: 传送门
- iOS - 基础面试知识
1.arc(automatic reference counting) OC对象被创建时引用计数从默认值0加1,当它被释放时候引用计数减1,引用计数减0时autorelease方法,销毁OC对象. 自 ...
- Linux Basis --- commands of vi
EDIT mode to GENERAL mode: press ESC general mode: CLOSE FILE :q! :force to close the file but no ...