当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql数据库连接池配置教程

mysql数据库连接池配置教程

2017年12月12日  | 移动技术网IT编程  | 我要评论

客家网,手工布鞋,守望者2.0.1

第一步:写javabean
复制代码 代码如下:

package withouttears.jdbc.db;
import java.util.hashmap;
import java.sql.*;
//jndi有两个核心接口context和dircontext,
//context中包含了基本的名字操作,而dircontext则将这些操作扩展到目录服务。
import javax.naming.context;
import javax.naming.initialcontext;
//数据库资源的连接工厂是javax.sql.datasource对象,
//它可以创建java.sql.connection数据库连接对象。
import javax.sql.datasource;
//目前您可以从java开发者连接(http://java.sun.com/products/jdbc/download.html#rowsetcobundle1_0)
//下载cachedrowset的实现。下载并解压缩安装文件后,将"rowset.jar"文件放到您的类目录下。
//cachedrowset在sun.jdbc.rowset包中。
import sun.jdbc.rowset.cachedrowset;
/**
* 作者:withouttears
* 时间:2006-12-13
* */
public class database {
/**************************************************************/
/* 函数:localhost
* 功能:建立连接池
* */
private static datasource localhost(){
datasource ds=null;
//在hashmap中通过get()来获取value,通过put()来插入value,
//containskey()则用来检验对象是否已经存在
hashmap<object,object> cachedds=new hashmap<object,object> ();
if(cachedds.containskey("ds"))//取出空闲状态的数据库连接
{
/* 在datasource中事先建立了多个数据库连接,
* 这些数据库连接保存在连接池(connect pool)中。
* java程序访问数据库时,只需要从连接池中取出空闲状态的数据库连接;
* 当程序访问数据库结束,再将数据库连接放回连接池。
* */
ds = (datasource)cachedds.get("ds");
}
else
try
{
/*在javax.naming包中提供了context接口,
* 该接口提供了将对象和名字绑定,以及通过名字检索对象的方法。
* */
context initctx = new initialcontext();
//lookup(string name):返回与指定的名字绑定的对象,获得数据库连接工厂
ds = (datasource)initctx.lookup("java:comp/env/jdbc/testdb");
cachedds.put("ds", ds);
}
catch(exception e)
{
e.printstacktrace();
}
return ds;
}
/**************************************************************/
/* 函数:getconnection
* 功能:库的连接
* */
private static connection getconnection(){
connection conn = null;
try
{
datasource ds = localhost();
conn = ds.getconnection();
}
catch(exception e)
{
e.printstacktrace();
}
return conn;
}
/**************************************************************/
/* 函数:close
* 功能:关闭连接
* */
private static void close(connection conn)
{
try
{
if(conn != null)
conn.close();
}
catch(sqlexception e)
{
e.printstacktrace();
}
}
/**************************************************************/
/* 函数:executequery
* 功能:数据查询
* */
public static cachedrowset executequery(string sql)
{
connection conn=null;
cachedrowset rs=null;
try{
rs=new cachedrowset();
conn=getconnection();
statement stmt=conn.createstatement();
resultset rs1=stmt.executequery(sql);
rs.populate(rs1);
}
catch(exception e)
{
//system.out.println(e.tostring());
}
finally{
try
{
conn.close();
}
catch(exception ex)
{}
} return rs;
}
/**************************************************************/
/* 函数:executeupdate
* 功能:数据更新(添加/更改/删除)
* */
public static boolean executeupdate(string sql)
{
boolean bl;
bl = false;
connection conn = getconnection();
try
{
statement stmt = conn.createstatement();
if(stmt.executeupdate(sql) > 0)
stmt.close();
bl = true;
}
catch(sqlexception e)
{
}
finally
{
close(conn);
}
return bl;
}
/**************************************************************/
}

编译得到withouttears/db/database.class并放到e:/myworkspace/test/web-inf/classes下,即e:/myworkspace/test/web-inf/classes/withouttears/db/database.class,注意别弄错了。
第二步:配置tomcat(我用是tomcat 5.5.7)
1. 在c:/program files/tomcat 5.5.7/conf/catalina/localhost下新建一个test.xml,内容如下: <context docbase="e:/myworkspace/test" path="/test"></context>
注:docbase为你的web文件所在地,我用的是e:/myworkspace/test。path可写可不写,但在linux下必须写上,windows下不写我测试可以用,最好写上。这里的test.xml指定的文件夹不像我们平时用的那样在c:/program files/tomcat 5.5.7/webapps/test,不过目的一样都是表示用http://localhost:8080/test/来访问,相当于iis下的虚拟目录,可以是任意的。
2. 在c:/program files/tomcat 5.5.7/conf/下建立context.xml,在e:/myworkspace/test下新建web-inf/web.xml。
context.xml
复制代码 代码如下:

<!-- the contents of this file will be loaded for each web application -->
<context>
<!-- default set of monitored resources -->
<watchedresource>web-inf/web.xml</watchedresource>
<watchedresource>meta-inf/context.xml</watchedresource>
<!-- uncomment this to disable session persistence across tomcat restarts -->
<!--
<manager pathname="" />
-->
<resource name="jdbc/testdb"
auth="container"
type="javax.sql.datasource"
driverclassname="com.mysql.jdbc.driver"
url="jdbc:mysql://localhost/mytestdb"
username="root"
password="157744375"
maxactive="100"
maxidle="30"
maxwait="10000"
/>
</context>

注: 链接池配置文件,这样我们就可以用第一步写的javabean类database中的localhost()函数来读取这个content.xml中的jdbc/testdb名称
web.xml
复制代码 代码如下:

<?xml version="1.0" encoding="gbk"?>
<web-app id="webapp_id" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
test</display-name>
<welcome-file-list>
<welcome-file>test.jsp</welcome-file>
</welcome-file-list>
<!-- jspc servlet mappings start -->
<!-- jspc servlet mappings end -->
</web-app>

注: web.xml放web的默认主页(如:test.jsp或者index.jsp)和程序中要用到servlet的映射,不管它我们这里用不到。
第三步:写test.jsp
复制代码 代码如下:

<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%>
<%@page import="java.sql.*"%>
<%@page import="withouttears.jdbc.db.*"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gbk">
<title>insert title</title>
</head>
<body>
<%
string sql=null;
sql="select * from table_test";
resultset rs=database.executequery(sql);
try{
while(rs.next()){
%>
姓名:<%=rs.getstring("name")%><br>
电话:<%=rs.getstring("mobile")%><br>
<%}}catch(exception e){} %>
</body>
</html>

第四步:测试

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网