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中引入了 ...
随机推荐
- 【Codeforces 723D】Lakes in Berland (dfs)
海洋包围的小岛,岛内的有湖,'.'代表水,'*'代表陆地,给出的n*m的地图里至少有k个湖,求填掉面积尽量少的水,使得湖的数量正好为k. dfs找出所有水联通块,判断一下是否是湖(海水区非湖).将湖按 ...
- bzoj3998: [TJOI2015]弦论
SAM小裸题qwq #include <iostream> #include <cstdio> #include <cmath> #include <cstr ...
- [日常训练]常州集训day8
T1 Description 给定一个长度为$n$的正整数序列$a$.可以将序列分成若干段,定义第$i$段的权值$x_i$为这一段中所有数的最大值,特殊地,$x_0=0$.求$\sum_{i=1}^{ ...
- 【BZOJ-4562】食物链 记忆化搜索(拓扑序 + DP)
4562: [Haoi2016]食物链 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 133 Solved: 112[Submit][Status] ...
- Enum遇到下拉框
package com.zj.tool; public enum WeekDay { Mon(), Tue(), Wed(), Thu(), Fri(), Sat(), Sun(); /**定义枚举类 ...
- 【uoj262】 NOIP2016—换教室
http://uoj.ac/problem/262 (题目链接) 题意 有${n}$个时间段,第${i}$个时间段可以选择在${c_i}$教室上课,也可以选择申请换课,有${k_i}$概率申请通过,在 ...
- win10 office2013激活工具
亲测可以用哦 下载
- MAC上快速调出终端的设置(保持和Windows的操作一致)
在Windows上可以这样操作[Win+R]键->输入[cmd/cmder]打开终端. 在MAC下需要做些设置:打开[系统偏好设置]->打开[键盘]->打开[快捷键]->找到[ ...
- hdu3932 模拟退火
模拟退火绝对是从OI--ACM以来接触过的所有算法里面最黑科技的orz 题意:地上有一堆hole,要找一个点,使得(距离该点最远的hole的距离)最小. sol:本来想套昨天的模拟退火模板,初值(0, ...
- 在c#中用指针操作图片像素点
在Bitmap类中有两个函数SetPixel,GetPixel,分别用来设置或读取图片中指定点的颜色(这里发现了VS的一个错误,SetPixel的文档说明写的是“获取颜色”??). 当要对一幅图进行相 ...