Java数据库连接池之proxool_动力节点Java学院整理

网友投稿 303 2023-04-23


Java数据库连接池之proxool_动力节点Java学院整理

Proxool是一种java数据库连接池技术。sourceforge下的一个开源项目,这个项目提供一个健壮、易用的连接池,最为关键的是这个连接池提供监控的功能,方便易用,便于发现连接泄漏的情况。

目前是和DBCP以及C3P0一起,最为常见的三种JDBC连接池技术。

日前,Hibernate官方宣布由于Bug太多不再支持DBCP,而推荐使用 Proxool或C3P0。

下面通过一个Demo说明一下如何使用:

项目结构如下:

DBLink.Java文件中的代码:

package com.bjpowernode.util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class DBLink {

private Connection connection;

private PreparedStatement preparedStatement;

private ResultSet resultSet;

static{

try {

Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); //加载驱动

}catch (ClassNotFoundException e) {

System.out.println("加载驱动失败!");

e.printStackTrace();

}

}

private Connection getConn(){

try {

if(connection==null||connection.isClosed()){

connection = DriverManager.getConnection("proxool.mysqlsource"); //获取连接

}

} catch (SQLException e) {

System.out.println("获取连接失败!");

e.printStackTrace();

}

return connection;

}

public List> select(String sql, Object [] params){

List> list = new ArrayList>();

try{

connection = this.getConn();

preparedStatement = connection.prepareStatement(sql);

if(params != null){

for(int i =0; i

preparedStatement.setObject(i+1, params[i]);//逐一给问号赋值

}

}

resultSet = preparedStatement.executeQuery();

ResultSetMetaData rsmd = resultSet.getMetaData();

int columnCount = rsmd.getColumnCount();

while(resultSet.next()){

Map map = new HashMap();

for(int i =1; i<=columnCount;i++){

String columnName = rsmd.getColumnName(i);

Object obj = null;

if(rsmd.getColumnTypeName(i).equals("DATE")){

obj = resultSet.getString(columnName);

}else{

obj = resultSet.getObject(columnName);

}

map.put(columnName, obj);

}

list.add(map);

}

}catch(SQLException e){

e.printStackTrace();

}finally{

close();

}

return list;

}

private void close(){

try {

if(resultSet != null){

resultSet.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

try {

ifhttp://(preparedStatement != null){

preparedStatement.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

try {

connection.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

proxool.xml文件中的代码:

mysqlsource

jdbc:mysql://localhost:3306/test

com.mysql.jdbc.Driver

60000

30

10

5

1

1

30s,10m,1d

ERROR

true

select CURRENT_DATE

web.xml文件中的代码:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

ServletConfigurator

org.logicalcobwebs.proxool.configuration.ServletConfigurator

xmlFile

WEB-INF/proxool.xml

1

index.jsp

index.jsp文件中的代码:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@page import="com.bjpowernode.util.DBLink,java.util.List,java.util.Map"%>

<head>

首页

preparedStatement.setObject(i+1, params[i]);//逐一给问号赋值

}

}

resultSet = preparedStatement.executeQuery();

ResultSetMetaData rsmd = resultSet.getMetaData();

int columnCount = rsmd.getColumnCount();

while(resultSet.next()){

Map map = new HashMap();

for(int i =1; i<=columnCount;i++){

String columnName = rsmd.getColumnName(i);

Object obj = null;

if(rsmd.getColumnTypeName(i).equals("DATE")){

obj = resultSet.getString(columnName);

}else{

obj = resultSet.getObject(columnName);

}

map.put(columnName, obj);

}

list.add(map);

}

}catch(SQLException e){

e.printStackTrace();

}finally{

close();

}

return list;

}

private void close(){

try {

if(resultSet != null){

resultSet.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

try {

ifhttp://(preparedStatement != null){

preparedStatement.close();

}

} catch (SQLException e) {

e.printStackTrace();

}

try {

connection.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

proxool.xml文件中的代码:

mysqlsource

jdbc:mysql://localhost:3306/test

com.mysql.jdbc.Driver

60000

30

10

5

1

1

30s,10m,1d

ERROR

true

select CURRENT_DATE

web.xml文件中的代码:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

ServletConfigurator

org.logicalcobwebs.proxool.configuration.ServletConfigurator

xmlFile

WEB-INF/proxool.xml

1

index.jsp

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

ServletConfigurator

org.logicalcobwebs.proxool.configuration.ServletConfigurator

xmlFile

WEB-INF/proxool.xml

1

index.jsp

index.jsp文件中的代码:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@page import="com.bjpowernode.util.DBLink,java.util.List,java.util.Map"%>

<head>

<%

String sql = "SELECT id, class_name, begin_date, end_date, state FROM class ";

DBLink db = new DBLink();

List> list = db.select(sql, null);

int index = 0;

for (Map map : list) {

index = index + 1;

DjHnvo int id = Integer.parseInt(map.get("id").toString());

String className = map.get("class_name").toString();

String beginDate = map.get("begin_date").toString();

String endDate = map.get("end_date").toString();

int state = Integer.parseInt(map.get("state").toString());

String stateName = (state == 0 ? "未启用" : "启用");

%>

<%

}

%>


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:开源 网关api(阿里开源网关)
下一篇:java仿枚举实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~