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中引入了 ...
随机推荐
- 【BZOJ-4353】Play with tree 树链剖分
4353: Play with tree Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 31 Solved: 19[Submit][Status][ ...
- springMVC-自定义数据类型转换器
自定义类型转换器 201603005,今天想了一个问题,Spring中的Conventer是如何实现的,因为他没有绑定类中的属性,它怎么知道要将那个String转换?看了几遍的书也没有找到,后来想想, ...
- ZOJ2314 Reactor Cooling
Reactor Cooling Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge The terrorist g ...
- ELF Executable Reconstruction From A Core Image
catalog . INTRODUCTION . THE PROCESS IMAGE . THE CORE IMAGE . EXECUTABLE RECONSTRUCTION . FAILURES I ...
- Linux fork()、exec() Hook Risk、Design-Principle In Multi-Threadeed Program
目录 . Linux exec指令执行监控Hook方案 . 在"Multi-Threadeed Program"环境中调用fork存在的风险 . Fork When Multi-T ...
- HDU 1394 Minimum Inversion Number(最小逆序数/暴力 线段树 树状数组 归并排序)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description The inve ...
- HDU 1166 敌兵布阵(线段树模板题)
题目链接: 传送门 敌兵布阵 Time Limit: 2000MS Memory Limit: 32768 K Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头 ...
- 旅图——UI测试
测试目标 保证代码质量,确保基础功能的实现,可以有效地保证代码的可靠性,让模块在与别的模块整合时出现更少的错误,减少最终测试时查找困难无方向. UI测试 登录过程 模拟登录过程,密码正确与密码错误的情 ...
- 【Alpha版本】冲刺-Day2
队伍:606notconnected 会议时间:11月10日 会议总结 张斯巍(433) 今天安排:把昨天没完成的做好,主界面图标的修改,侧边栏背景设计 完成度:85% 明天计划:个人信息界面设计.优 ...
- MYSQL入门(三)
索引简介 索引是对数据库表中一个或多个列(例如,employee 表的姓名 (name) 列)的值进行排序的结构.如果想按特定职员的姓来查找他或她,则与在表中搜索所有的行相比,索引有助于更快地获取信息 ...