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中引入了 ...
随机推荐
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)【转载】
最近在学习Spring+SpringMVC+MyBatis的整合.以下是参考网上的资料自己实践操作的详细步骤. 1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于20 ...
- Java多线程与并发库高级应用-线程池
线程池 线程池的思想 线程池的概念与Executors类的应用 > 创建固定大小的线程池 > 创建缓存线程池 > 创建单一线程池(如何实现线程死掉后重新启动?) 关闭线程池 > ...
- 启动Hadoop HA Hbase zookeeper spark
服务器角色 服务器 192.168.58.180 192.168.58.181 192.168.58.182 192.168.58.183 Name CentOSMaster Slvae1 Slave ...
- win10 office2013激活工具
亲测可以用哦 下载
- iOS 关于本地持久化存储的探讨
目前,用以本地化存储的方式有很多,常用的有以下: 1.临时缓存 先说说临时缓存,临时缓存一般相当于用来管理应用程序中全局需要常用的一些内容.比如当前用户的ID或者当前的定位信息等. 常用的方式就是写一 ...
- shell脚本批量调用git命令
有时候想对本地的几个repository都进行一下pull,一个一个操作比较繁琐,所以写了个shell脚本进行简化操作. git_pull_all.sh #!/bin/sh clear functio ...
- 加州大学伯克利分校Stat2.3x Inference 统计推断学习笔记: Section 1 Estimating unknown parameters
Stat2.3x Inference(统计推断)课程由加州大学伯克利分校(University of California, Berkeley)于2014年在edX平台讲授. PDF笔记下载(Acad ...
- codevs 1229 数字游戏(可重集的全排列)
传送门 Description Lele 最近上课的时候都很无聊,所以他发明了一个数字游戏来打发时间. 这个游戏是这样的,首先,他拿出几张纸片,分别写上0到9之间的任意数字(可重复写某个数字),然后 ...
- [Android]Volley源码分析(一)
一. 如何使用Volley? 1. 首先定义一个RequestManager类,用来在Android程序启动时对Volley进行初始化.RequestManager为单例类,因为只有在程序启动时调用, ...
- WPF元素可视化效果
在WPF中设置元素的可视化效果主要用到BlurEffect类和DropShadowEffect类.(目前只学到这两个,哈哈) 1.BlurEffect类 命名空间: System.Windows.Me ...