Proxool建立数据库连接池(源码)

          最近搞数据库连接池,着实很让我郁闷,首先是网上很少有这方面权威的中文资料,官方网址上估计有,但是我英语不行,看不懂。所以一边请教身边的人,

 

一边摸索,最后还是在别人的指导下,搞出来了,但是让我郁闷的是这个项目在别人的机子上可以运行,但是在我的机子上,改数据库表名,sql文件,和一些配置

 

后,就是没法在我机子上实现,明明没问题的语句,但就是执行出错。

 

不过也巧了,有些在别人机子不能正常运行的项目,却在我的机子上可以运行,估计还是环境问题,可我现在也不具备解决这种问题的能力,所以只有不断的重试,

 

不断的郁闷,我也总不能因为环境不同,而把MyEclipse,mysql,jdk换成低版本的吧。所以这些问题还是没解决。

 

因为这些原因,有还几个小项目的功能没实现,心里也堵得慌。看来搞Java真能锻炼忍耐力啊。

 

发泄完了,切入正题:

 

Proxool是一个Java SQL Driver驱动程序,提供了对你选择的其它类型的驱动程序的连接池封装。可以非常简单的移植到现存的代码中。完全可配置。快速,成熟,

 

健壮。可以透明地为你现存的JDBC驱动程序增加连接池功能。

 

环境:MyEclipse连接mysql

 

实现功能:网络留言板

 

实现这个东西需要两个jar包,这里提供下载,分别为proxool-0.9.0RC3.jar,commons-logging.jar。

 

还需要一个proxool.xml的配置文件代码:

 

ContractedBlock.gifExpandedBlockStart.gif Code
<?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://localhost:3306/mysql</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root"/>
<property name="password" value="840815"/>
</driver-properties>
<maximum-connection-count>100</maximum-connection-count>
<house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
</proxool>
</something-else-entirely>

 

还需要在web.xml文件中调用,代码:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns
="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    
<servlet-name>AddMessageServlet</servlet-name>
    
<servlet-class>webbook.guestbook.AddMessageServlet</servlet-class>
  
</servlet>
  
<servlet>
    
<servlet-name>GetMessagesServlet</servlet-name>
    
<servlet-class>webbook.guestbook.GetMessagesServlet</servlet-class>
  
</servlet>
 
   
<servlet-mapping>
    
<servlet-name>AddMessageServlet</servlet-name>
    
<url-pattern>/servlet/addMessage</url-pattern>
  
</servlet-mapping>
  
<servlet-mapping>
    
<servlet-name>GetMessagesServlet</servlet-name>
    
<url-pattern>/servlet/getMessages</url-pattern>
  
</servlet-mapping>
  
    
<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/proxool.xml</param-value>
    
</init-param>
    
<load-on-startup>1</load-on-startup>
  
</servlet>
  
<servlet>
    
<servlet-name>Admin</servlet-name>
    
<servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
  
</servlet>
    
<servlet-mapping>
    
<servlet-name>Admin</servlet-name>
    
<url-pattern>/admin</url-pattern>
  
</servlet-mapping>
  
<welcome-file-list>
  
<welcome-file>addMessage.htm</welcome-file>
  
</welcome-file-list>


</web-app>

 

还需要一个类文件,Access.java,代码:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
package webbook.guestbook;

import java.sql.*;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import webbook.util.StringUtil;

ExpandedBlockStart.gifContractedBlock.gif
public class Access {
    
private Connection con = null;

    
private Statement sta = null;

    
private ResultSet rs = null;

    
private PreparedStatement pstmt = null;
    
private HttpServletRequest request = null;
    
private int result = 0;

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*    private String driverName="org.gjt.mm.mysql.Driver";

     private String url="jdbc:mysql://10.187.202.153:3306/hicctsc";

     private String userName="root";

     private String userPassword="hicc";
     
*/


ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void loadMyDriver() {

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setMyConnection() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            
//  this.con=DriverManager.getConnection(url,userName,userPassword);
            this.con = DriverManager.getConnection("proxool.mysql");
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SQLException e1) {
            System.out.println(e1.getMessage());
        }


    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void createMyStatement() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            
this.sta = this.con
                    .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                            ResultSet.CONCUR_READ_ONLY);
            
//            ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY
ExpandedSubBlockStart.gifContractedSubBlock.gif
        }
 catch (SQLException e2) {
            System.out.println(e2.getMessage());
        }

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void createMyPreparedStatement(String sql, HttpServletRequest request) {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            
this.request = request;
            
this.pstmt = this.con.prepareStatement(sql);
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SQLException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
public ResultSet executeMyQuery(String sqlWords) {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            
this.rs = this.sta.executeQuery(sqlWords);
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SQLException e3) {
            System.out.println(e3.getMessage());
        }

        
return rs;
    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void executeMyUpdata(String sqlWords2) {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            sta.executeUpdate(sqlWords2);
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SQLException e4) {
            System.out.println(e4.getMessage());
        }

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
public int executeMyUpdata1() {

ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {

            pstmt.setString(
1, StringUtil.filterHtml(request
                    .getParameter(
"name")));

            pstmt.setString(
2, StringUtil.filterHtml(request
                    .getParameter(
"email")));

            pstmt.setString(
3, StringUtil.filterHtml(request
                    .getParameter(
"phone")));
            pstmt.setString(
4, StringUtil.filterHtml(request
                    .getParameter(
"title")));
            pstmt.setString(
5, request.getParameter("content"));

            SimpleDateFormat sdf 
= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            pstmt.setString(
6, sdf.format(new java.util.Date()));
            result 
= pstmt.executeUpdate();

ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (Exception e) {
            e.printStackTrace();
        }

        
return result;
    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void closeMyResultSet() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            
this.rs.close();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SQLException e5) {
            System.out.println(e5.getMessage());
        }

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void closeMyStatement() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            
this.sta.close();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SQLException e6) {
            System.out.println(e6.getMessage());
        }

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void closeMyPreparedStatement() {

ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            
this.pstmt.close();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (NullPointerException e) {
            e.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void closeMyConnection() {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try {
            
this.con.close();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (SQLException e7) {
            System.out.println(e7.getMessage());
        }

    }


ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String args[]) throws SQLException {
        Access sh 
= new Access();
        
//sh.loadMyDriver();
        sh.setMyConnection();
        sh.createMyStatement();
        ResultSet rs 
= sh.executeMyQuery("select * from GLY");
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while (rs.next()) {
            System.out.println(rs.getString(
2));
        }

        sh.closeMyConnection();
    }


}

 

下面只需要在addMessage.java中调用这个类方法就可以了

 

施杨出品!!!

 

如果运行出现问题,欢迎留言反映。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值