不用spring框架,只使用jdbcTemplate + proxool

jdbcTemplate + proxool

只使用jdbcTemplate, 连接池用proxool.

1.下载地址
proxool:
http://proxool.sourceforge.net/



2.导入的包
asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
log4j-1.2.16.jar
mysql-connector-java-5.1.8-bin.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.jdbc-3.1.1.RELEASE.jar
org.springframework.transaction-3.1.1.RELEASE.jar
proxool-0.9.1.jar
proxool-cglib.jar

3.配置文件 /WEB-INF/proxool.xml



dbname
jdbc:mysql://118.186.210.242:447/wucaiju?user=root&password=root&useUnicode=true&characterEncoding=utf-8
com.mysql.jdbc.Driver




100
10
30000
10
5



4.web.xml


ServletConfigurator

org.logicalcobwebs.proxool.configuration.ServletConfigurator


xmlFile
WEB-INF/proxool.xml

1


datasource_dbname
org.logicalcobwebs.proxool.admin.servlet.AdminServlet


datasource_dbname
/datasource_dbname




proxool
/datasource_dbname


manager



BASIC
proxool manager Application



The role that is required to log in to the Manager
Application

manager



5.测试类
public class TestDao {

public void test()throws Exception{
Context context = new InitialContext();
//DataSource ds = (DataSource)context.lookup("java:/comp/env/jdbc/mysql");
//DataSource ds = (DataSource)context.lookup("proxool.dbname");
DataSource ds = new ProxoolDataSource("dbname") ; //此处是alias的名称,而不是"proxool.别名"

JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
jdbcTemplate.afterPropertiesSet();
System.out.println(jdbcTemplate);

String sql = "select * from t_pic";
List list = jdbcTemplate.queryForList(sql);

for(int i=0; i Map map = (Map)list.get(i);
System.out.println(map.get("createTime"));
}
}
}


6.直接jdbc用proxool

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.ProxoolFacade;
import org.logicalcobwebs.proxool.admin.SnapshotIF;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;

public class PoolManager{

private static int activeCount = 0 ;


public PoolManager(){

}
/**
* 获取连接
* getConnection
* @param name
* @return
*/
public Connection getConnection() {
try {
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver" ); //proxool驱动类
Connection conn = DriverManager.getConnection("proxool.dbname" ); // 注意,此处用的是proxool + 别名
//此处的datasource1是在proxool.xml中配置的连接池别名,当然根据需要可以用datasource2
showSnapshotInfo();

return conn;
}catch (Exception ex){
ex.printStackTrace();
}
return null ;
}
/**
* 此方法可以得到连接池的信息
* showSnapshotInfo
*/
private void showSnapshotInfo(){
try {
SnapshotIF snapshot = ProxoolFacade.getSnapshot("dbname" , true );
int curActiveCount=snapshot.getActiveConnectionCount(); //获得活动连接数
int availableCount=snapshot.getAvailableConnectionCount(); //获得可得到的连接数
int maxCount=snapshot.getMaximumConnectionCount() ; //获得总连接数
if (curActiveCount!=activeCount) //当活动连接数变化时输出的信息
{
System.out.println("活动连接数:" +curActiveCount+ "(active) 可得到的连接数:" +availableCount+ "(available) 总连接数:" +maxCount+ "(max)" );
activeCount=curActiveCount;
}
}catch (ProxoolException e){
e.printStackTrace();
}
}
/**
* 获取连接
* getConnection
* @param name
* @return
*/
public Connection getConnection(String name){
return getConnection();
}
/**
* 释放连接
* freeConnection
* @param conn
*/
public void freeConnection(Connection conn){
if (conn!= null ){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 释放连接
* freeConnection
* @param name
* @param con
*/
public void freeConnection (String name,Connection con){
freeConnection(con);
}

public void getQuery() {
try {
Connection conn = getConnection();
if (conn != null ){
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select * from t_pic" );
int c = rs.getMetaData().getColumnCount();
while (rs.next()){
System.out.println();
for ( int i= 1 ;i<=c;i++){
System.out.print(rs.getObject(i));
}
}
rs.close();
}
freeConnection(conn);
} catch (SQLException e) {
e.printStackTrace();
}

}

public static void main(String args[]){
PoolManager pm = new PoolManager();
pm.getQuery();
}

}


你可能感兴趣的:(不用spring框架,只使用jdbcTemplate + proxool)