数据库连接池问题conn cannot be resolved!
frice 2006-09-05 10:14:50 server.xml 配置:
<host>
...
<Context path="/test" docBase="test"
debug="5" reloadable="true" crossContext="true">
<Resource
name="jdbc/conMysql"
type="javax.sql.DataSource"
password="mypass"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost/mysql"
maxActive="8"/>
</Context>
</host>
---------------------------------------------------------------------------
webapps\test\WEB-INF\web.xml 配置:
<web-app>
...
<resource-ref>
<description>Tomcat Datasource</description>
<res-ref-name>jdbc/conMysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
------------------------------------------------------------------------------
jsp 代码:
<%@ page language="java" %>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*,javax.naming.Context,javax.naming.InitialContext"%>
<%@ page import="java.sql.*,javax.sql.DataSource"%>
<html>
<head>
<title>JSP连接MYSQL数据池</title>
<head>
<body>
<%
//声名
Statement stmt = null;
ResultSet rs = null;
try
{
// Obtain our environment naming context
Context initCtx = new InitialContext();
if ( initCtx == null ) {
throw new Exception("Uh oh -- no context!");
}
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our data source
DataSource ds = (DataSource) envCtx.lookup("jdbc/conMysql");
if (ds == null)
{
throw new Exception("Data source not found!");
}
else
{
Connection conn = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * From czy");
while (rs.next())
{
%>
<%=rs.getString("czyid")%>
<%=rs.getString("name")%>
<%=rs.getString("pwd")%><br/>
<%
}
}
rs.close(); //关闭ResultSet对象
stmt.close(); //关闭Statement对象
conn.close(); //关闭Connection对象
}
catch (Exception e)
{
out.println(e);
}
%>
</body>
</html>