您的位置:首页 > 运维架构 > Tomcat

tomcat配置dbcp数据库连接池及其碰到的错误

2008-08-25 09:32 288 查看
在jsp中配置数据库连接池的步骤.

1.要在tomcat下的conf包中的server.xml中加入数据库连接池配置信息

<Context path="/jdbc" docBase="D:/workspace/jdbc/WebRoot" >

<Resource auth="Container" name="jdbc/hhh" type="javax.sql.DataSource" factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"

driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/hanyue"

username="root"

password=""

maxActive="10000"

maxIdle="10000"

maxWait="10000"

removeAbandoned="true"

removeAbandonedTimeOut="10"

logAbandoned="true"/>

</Context>

1.确认common/lib/naming-factory-dbcp.jar存在.添加factory的时候一定要写org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory,

而不能写org.apache.commons.dbcp.BasicDataSourceFactory,否则会报找不到驱动程序的url的错误.

2.注意:

path:Web应用程序的上下文路径。(就是你的项目 名前加上以个 “/”)
docBase:表示的项目的具体路径,如果你的项目是在D:/demo,这里就应该是D:/Demo
该测试项目路径D:/workspace/jdbc/WebRoot
如果测试项目的文件位置,是在tomcat/webapps/Demo中,所以属性为Demo
< Resource >元素为JNDI,在lookup是要查找的资源
name: 表示JNDI在lookup是输入的 资源名
3.

<%@ page import="java.sql.*" %>

<%@ page import="javax.sql.*" %>

<%@ page import="javax.naming.*" %>

<%@ page contentType="text/html;charset=GB2312" %>

<html>

<head>

<title>my jdbc action</title>

</head>

<body>

<h2>登陆结果</h2>

<%

Connection con = null;

Statement stmt = null;

Statement stmt1 = null;

ResultSet rs = null;

String url = "jdbc:mysql://localhost:3306/hanyue";

String uname = "root";

String upasswd = "";

DataSource ds = null;

String id = "13";

String name = request.getParameter("name");

String passwd = request.getParameter("passwd");

String new_last_name = "";

String new_first_name = "";

%>

从login.html得到值

name = <%= name %>

passwd = <%= passwd %><br><br>

<%

try

{

//找到驱动程序并注册

Context initContext = new InitialContext();

Context envContext = (Context)initContext.lookup("java:/comp/env");

DataSource dst = (DataSource)envContext.lookup("jdbc/hhh");

//ds = (DataSource) initContext.lookup("java:comp/env/jdbc/hhh");

con = dst.getConnection();

if(envContext==null){

System.out.println("envContext is null");

}else{

System.out.println("envContext is not null");

}

if(dst==null){

System.out.println("dst is null");

}else{

System.out.println("dst is not null");

}

if(con==null){

System.out.println("con is null");

}else{

System.out.println("con is not null");

}

//stmt = con.createStatement();

// Class.forName("org.gjt.mm.mysql.Driver");

// con = DriverManager.getConnection(url,uname,upasswd);

// stmt = con.createStatement();

//String upd = "insert into person values('"+id+"','"+name+"','"+passwd+"')";

//stmt.executeUpdate(upd);

stmt1 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

String query = "SELECT * FROM person";

rs = stmt1.executeQuery(query);

rs.last( );

new_last_name = rs.getString("name");

new_first_name = rs.getString("password");

}

catch(SQLException e){

e.printStackTrace();

}

finally

{

try {

if(con != null)

{

con.close();

}

}

catch(SQLException sqle)

{

out.println("sqle="+sqle);

}

}

%>

从数据库中取得的数据:

<% out.println("name = "+new_last_name);

out.println("passwd = "+new_first_name);%>

</body>

</html>

测试界面:

<html>
<head>
<title>my jdbc</title>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>
<body>

<h2>登陆信息</h2>
<form name="form" action="action.jsp" method="post" >
<p><input name="name" type="text"></p>
<p><input name="passwd" type="password"></p>
<p>
<input type="submit" value="确定">
<input type="reset" value="清空">
</p>
</form>

</body>
</html>

数据库用mysql...测试结果...打印出了刚存入的数据.数据库连接池测试成功!!!!

问题:为什么用org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory的不行.

为什么该工程下的web.xml的东西注册不注册都行???

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/hanyue</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: