初学proxool

菜鸟一枚,学习proxool配置管理数据库连接。

新建了一个application程序测试proxool数据库连接。

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>dbname</alias> <!--数据源的别名-->
		<driver-url>jdbc:oracle:thin:@127.0.0.1:1521:orcl</driver-url><!--url连接串-->
		<driver-class>oracle.jdbc.driver.OracleDriver</driver-class> <!--驱动类-->
		<driver-properties>
			<property name="user" value="zen" /> <!--用户名-->
			<property name="password" value="sa" /><!--密码-->
		</driver-properties> 
		<!--最大连接数(默认5个),超过了这个连接数,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定 -->
		<maximum-connection-count>100</maximum-connection-count> 
		<!--最小连接数(默认2个)-->
		<minimum-connection-count>10</minimum-connection-count> 
		<!--proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁 默认30秒-->
		<house-keeping-sleep-time>90000</house-keeping-sleep-time>
		<!--没有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的用户连接就不会被接受-->
		<maximum-new-connections>10</maximum-new-connections> 
		<!--最少保持的空闲连接数(默认2个)-->
		<prototype-count>5</prototype-count> 
		<!--在使用之前测试-->
		<test-before-use>true</test-before-use>
		<!--用于保持连接的测试语句 -->
		<house-keeping-test-sql>select sysdate from dual</house-keeping-test-sql>
	</proxool>
</something-else-entirely> 

oracle数据库中,测试表userinfo对应的bean:userinfo.java

package cn.proxool;

import java.util.Date;

/**
 * @author xxx
 *
 */
public class UserInfo {
	 private int userid;
	   private String name;
	   private String userPwd;
	   private Date birthday;

	   public UserInfo(int id)
	   {
	       this.userid=id;
	   }
	    public UserInfo(int userid, String userName, String userPwd, Date birthday)
	    {
	        super();
	        this.userid = userid;
	        this.name = userName;
	        this.userPwd = userPwd;
	        this.birthday = birthday;
	    }
	    //构造器
	    public UserInfo()
	    {
	    }
		public int getUserid() {
			return userid;
		}
		public void setUserid(int userid) {
			this.userid = userid;
		}
		public String getUserName() {
			return name;
		}
		public void setUserName(String userName) {
			this.name = userName;
		}
		public String getUserPwd() {
			return userPwd;
		}
		public void setUserPwd(String userPwd) {
			this.userPwd = userPwd;
		}
		public Date getBirthday() {
			return birthday;
		}
		public void setBirthday(Date birthday) {
			this.birthday = birthday;
		}
	    
}

数据库连接控制类:SqlTool.java

package cn.proxool;

import java.sql.*;
import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;

public final class SqlTool{

 private SqlTool(){
 }
 
 private static void init(){
  try {
   JAXPConfigurator.configure("src/proxool.xml",false);// proxool.xml所在目录
     }
     catch (Exception ex) {
      ex.printStackTrace();
     }
 }
 /**
     * get Connection
     */
 public static Connection getConnection(){
  Connection conn=null;
  try{
   init();
   conn=DriverManager.getConnection("proxool.dbname");
  }
  catch(Exception e){
   conn=null;
  }
  return conn;
 }
 
 /**
     * closeConnection
     */
 public static void closeConnection(Connection conn){
  try{
   if((conn!=null)&&(!conn.isClosed())){
    conn.close();
   }
  }
  catch(SQLException e){
   System.out.println("close Connection error!");
  }
 }
 
 /**
     * closePreparedStatement
     */
 public static void closePreparedStatement(PreparedStatement pstmt){
  try{
   if(pstmt!=null){
    pstmt.close();
   }
  }
  catch(SQLException e){
   System.out.println("close PreparedStatement error!");
  }
 }
 
 /**
     * closeResultSet
     */
 public static void closeResultSet(ResultSet rs){
  try {
   if(rs!=null){
    rs.close();
   }
     }
     catch (Exception ex) {
      System.out.println("close ResultSet 异常!");
     }
 }
 
 
 /**
     * 测试main
     */
 public static void main(String[] args)throws Exception{
  java.sql.Connection conn=SqlTool.getConnection();
  System.out.println (conn==null?"NULL":"NOT NULL");
 }
}

数据库操作类:UserTool.java


package cn.proxool;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.apache.log4j.Logger;

public class UserTool {
	/** log */
	private static Logger logger = Logger.getLogger(UserTool.class);
	private static UserTool instance;

	// constructor
	private UserTool() {
	}

	/**
	 * get instance
	 * 
	 * @return the single instance
	 */
	public static UserTool getInstance() {
		if (instance == null) {
			instance = new UserTool();
		}
		return instance;
	}

	/**
	 * print the user info
	 * 
	 * @param userInfo
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 */
	@SuppressWarnings("unchecked")
	public void printUserInfo(UserInfo userInfo) {
		// fields
		Field[] fields = UserInfo.class.getDeclaredFields();

		Field field = null;
		// for each
		for (int i = 0; i < fields.length; i++) {
			field = fields[i];
			String name = field.getName();
			Object value = null;
			Class type = field.getType();
			// try catch
			try {
				field.setAccessible(true);
				if (type == int.class) {
					value = (int) field.getInt(userInfo);
				} else if (type == String.class) {
					value = (String) field.get(userInfo);
				} else if (type == Date.class) {
					value = (Date) field.get(userInfo);
				}
				logger.info(name + "->" + value);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * find User Info By Id
	 * @param id
	 * @return UserInfo
	 */
	@SuppressWarnings("unchecked")
	public UserInfo findUserInfoById(int id) {//该方法这样写,源于数据库字段和对应bean的字段是一样的
		Connection connection = SqlTool.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		if (null == connection) {
			logger.warn("connection is null!");
			return null;
		}
		// user info
		UserInfo userInfo = null;
		Field[] fields = UserInfo.class.getDeclaredFields();
		Class type = null;
		String fieldName = null;
		try {
			pstmt = connection
					.prepareStatement("select * from userinfo where USERID=?");
			pstmt.setInt(1, id);
			// execute sql
			rs = pstmt.executeQuery();

			while (rs.next()) {
				userInfo = new UserInfo();
				Field field = null;
				for (int i = 0; i < fields.length; i++) {
					field = fields[i];
					// get field name
					fieldName = field.getName();
					// get field type
					type = field.getType();
					// accessible
					field.setAccessible(true);
					try {
						if (type == int.class) {
							field.setInt(userInfo, rs.getInt(fieldName));
						} else if (type == String.class) {
							field.set(userInfo,rs.getString(fieldName));
						}else if(type == Date.class){
							field.set(userInfo, rs.getDate(fieldName));
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			SqlTool.closeResultSet(rs);
			SqlTool.closePreparedStatement(pstmt);
			SqlTool.closeConnection(connection);
		}

		return userInfo;
	}

	public static void main(String[] args) {
		logger.info("UserTool start...");
		UserTool tool = UserTool.getInstance();
		UserInfo userInfo = tool.findUserInfoById(1);
		tool.printUserInfo(userInfo);
		logger.info("-----> end");
	}
}

所需jar包:proxool-0.9.1.jar,log4j.jar,commons-logging.jar,commons-pool.jar,proxool-cglib.jar,ojdbc14.jar


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
proxool.xml 是一个配置文件,用于配置 Proxool 数据库连接池。Proxool 是一个开源的 Java 数据库连接池,它允许应用程序在运行时动态地管理数据库连接,以提高应用程序的性能和可靠性。 proxool.xml 文件包含以下信息: 1. 数据库驱动程序的类名和连接字符串。 2. 数据库连接池的配置信息,例如最大连接数、最小连接数、最大空闲时间等。 3. 用户名和密码等连接数据库所需的凭证信息。 proxool.xml 文件的示例代码如下: ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE proxool PUBLIC "-//mikebob.net/proxool//DTD Proxool//EN" "proxool.dtd"> <proxool> <driver-url>jdbc:mysql://localhost/mydb</driver-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user>myuser</user> <password>mypassword</password> <maximum-connection-count>50</maximum-connection-count> <minimum-connection-count>10</minimum-connection-count> <simultaneous-build-throttle>20</simultaneous-build-throttle> <house-keeping-test-sql>select 1</house-keeping-test-sql> <house-keeping-sleep-time>30000</house-keeping-sleep-time> </proxool> ``` 其中,`driver-url`、`driver-class`、`user`、`password` 分别表示数据库连接字符串、数据库驱动程序的类名、用户名和密码;`maximum-connection-count`、`minimum-connection-count` 分别表示连接池的最大连接数和最小连接数;`simultaneous-build-throttle` 表示同时建立连接的最大数目;`house-keeping-test-sql` 表示定期检查连接是否可用的 SQL 语句;`house-keeping-sleep-time` 表示定期检查连接的时间间隔。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值