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中引入了 ...
随机推荐
- 使用IDEA自带的rest client参数传递乱码问题
在idea使用rest client(idea自带的访问请求地址工具)调试的时候,有时出现中文会乱码的情况 解决方法如下: 1.打开IDEA安装路径下的bin文件夹里面的idea64.ex ...
- VirtualBox提示:错误,创建一个新任务失败,被召者解决办法
被召者 RC: REGDB_E_CLASSNOTREG (0x80040154) 目前有两种解决办法: 解决方法一: 打开命令窗口(快捷键:窗口键+R,输入cmd点击确定) 输入cd D:\Progr ...
- c#读取excel
Provider根据实际EXCEL的版本来设置,推荐使用ACE接口来读取.需要Access database Engine. 注意修改注册表以下两项的值为0.否则导入EXCEL当单元格内字符长度超过2 ...
- python3 连接SQLserver
pymssql 是Python语言用来连接微软 SQL SERVER 数据库的类库,实现了 Python DB API 2.0 .官网介绍. 最简事例: #!/usr/bin/env python # ...
- Hadoop 学习笔记3 Develping MapReduce
小笔记: Mavon是一种项目管理工具,通过xml配置来设置项目信息. Mavon POM(project of model). Steps: 1. set up and configure the ...
- Jenkins环境拓扑及部署流程
环境拓扑图: 部署流程:
- Bzoj2648 SJY摆棋子
Time Limit: 20 Sec Memory Limit: 128 MB Submit: 3128 Solved: 1067 Description 这天,SJY显得无聊.在家自己玩.在一个 ...
- duxcms SQL Injection In /admin/module/loginMod.class.php
目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 duxcms是一款采用PHP开发,基于HMVC规则开发适合中小企业.公司.新闻.个 ...
- J-link烧写brjtag工具
J-Link用的山寨货,不知道山寨了几代的那种....用的STM32F103C8T6的小板也是山寨了好几代那种,才25块钱...好在能用,J-Link用segger公司的软件能识别,也能找到CPU,板 ...
- 解决 Ubuntu 无法调节屏幕亮度的问题(转)
转http://blog.163.com/tym190@126/blog/static/8776005920143192412477/ 终端输入代码: 在打开文件中找到 GRUB_CMDLIN ...