数据库连接池问题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>
...全文
1975 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
frice 2006-09-06
  • 打赏
  • 举报
回复
谢谢
test_1982 2006-09-05
  • 打赏
  • 举报
回复
你上面回出现编译错误是因为Connection conn = ds.getConnection();这个申明是在一个判断语句里面的。但是外面你却用到了。conn.close();你要把上面的这个申明放到判断语句外面。
frice 2006-09-05
  • 打赏
  • 举报
回复
再顶!
frice 2006-09-05
  • 打赏
  • 举报
回复
驱动有啊 我直接在jsp 文件中连接数据库可以啊.
<%@ page language="java" %>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*,javax.sql.*"%>
<html>
<head>
<title>JSP连接MYSQL数据库</title>
<head>
<body>
<%
//声名
String driver="com.mysql.jdbc.Driver";
String sConnStr ="jdbc:mysql://localhost/mysql";
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(sConnStr,"root","mypass");
Statement stmt = null;
ResultSet rs = null;
String sql="select pwd from czy where czyID = '001' ";

stmt = conn.createStatement();
rs = stmt.executeQuery(sql);

//利用while循环将数据表中的记录列出
while (rs.next())
{
%>
<%=rs.getString("pwd")%><br/>
<%
}
rs.close(); //关闭ResultSet对象
stmt.close(); //关闭Statement对象
conn.close(); //关闭Connection对象
%>
weiba9412 2006-09-05
  • 打赏
  • 举报
回复
你用的是Tomcat吧!?你在common\lib\下放了MYsql的驱动包没有?2个......
frice 2006-09-05
  • 打赏
  • 举报
回复
关注!
frice 2006-09-05
  • 打赏
  • 举报
回复
自己UP一下!
frice 2006-09-05
  • 打赏
  • 举报
回复
错误代码:

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 44 in the jsp file: /second2.jsp
Generated servlet error:
conn cannot be resolved


org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
feettrace 2006-09-05
  • 打赏
  • 举报
回复
你的Connection conn = ds.getConnection();放在else里啊,要是不执行这句,后面就没conn,那conn.close(); 从何而来

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧