Proxool是一个Java SQL Driver驱动程序,提供了对你选择的其它类型的驱动程序的连接池封装。可以非常简单的移植到现存的代码中。完全可配置。快速,成熟,健壮。可以透明地为你现存的JDBC驱动程序增加连接池功能。
最近搞数据库连接池,着实很让我郁闷,首先是网上很少有这方面权威的中文资料,官方网址上估计有,但是我英语不行,看不懂。所以一边请教身边的人,
一边摸索,最后还是在别人的指导下,搞出来了,但是让我郁闷的是这个项目在别人的机子上可以运行,但是在我的机子上,改数据库表名,sql文件,和一些配置
后,就是没法在我机子上实现,明明没问题的语句,但就是执行出错。
不过也巧了,有些在别人机子不能正常运行的项目,却在我的机子上可以运行,估计还是环境问题,可我现在也不具备解决这种问题的能力,所以只有不断的重试,
不断的郁闷,我也总不能因为环境不同,而把MyEclipse,mysql,jdk换成低版本的吧。所以这些问题还是没解决。
因为这些原因,有还几个小项目的功能没实现,心里也堵得慌。看来搞Java真能锻炼忍耐力啊。
发泄完了,切入正题:
Proxool是一个Java SQL Driver驱动程序,提供了对你选择的其它类型的驱动程序的连接池封装。可以非常简单的移植到现存的代码中。完全可配置。快速,成熟,
健壮。可以透明地为你现存的JDBC驱动程序增加连接池功能。
环境:MyEclipse连接mysql
实现功能:网络留言板
实现这个东西需要两个jar包,这里提供下载,分别为proxool-0.9.0RC3.jar,commons-logging.jar。
还需要一个proxool.xml的配置文件代码:
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文件中调用,代码:
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,代码:
Code
package webbook.guestbook;
import java.sql.*;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import webbook.util.StringUtil;
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;
/**//* 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";
*/
public void loadMyDriver() {
}
public void setMyConnection() {
try {
// this.con=DriverManager.getConnection(url,userName,userPassword);
this.con = DriverManager.getConnection("proxool.mysql");
} catch (SQLException e1) {
System.out.println(e1.getMessage());
}
}
public void createMyStatement() {
try {
this.sta = this.con
.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY
} catch (SQLException e2) {
System.out.println(e2.getMessage());
}
}
public void createMyPreparedStatement(String sql, HttpServletRequest request) {
try {
this.request = request;
this.pstmt = this.con.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet executeMyQuery(String sqlWords) {
try {
this.rs = this.sta.executeQuery(sqlWords);
} catch (SQLException e3) {
System.out.println(e3.getMessage());
}
return rs;
}
public void executeMyUpdata(String sqlWords2) {
try {
sta.executeUpdate(sqlWords2);
} catch (SQLException e4) {
System.out.println(e4.getMessage());
}
}
public int executeMyUpdata1() {
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();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public void closeMyResultSet() {
try {
this.rs.close();
} catch (SQLException e5) {
System.out.println(e5.getMessage());
}
}
public void closeMyStatement() {
try {
this.sta.close();
} catch (SQLException e6) {
System.out.println(e6.getMessage());
}
}
public void closeMyPreparedStatement() {
try {
this.pstmt.close();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void closeMyConnection() {
try {
this.con.close();
} catch (SQLException e7) {
System.out.println(e7.getMessage());
}
}
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");
while (rs.next()) {
System.out.println(rs.getString(2));
}
sh.closeMyConnection();
}
}
下面只需要在addMessage.java中调用这个类方法就可以了
施杨出品!!!
如果运行出现问题,欢迎留言反映。