当前页面: 开发资料首页 → JSP 专题 → 数据库连接池问题conn cannot be resolved!
数据库连接池问题conn cannot be resolved!
摘要: 数据库连接池问题conn cannot be resolved!
server.xml 配置:
...
debug="5" reloadable="true" crossContext="true">
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"/>
---------------------------------------------------------------------------
webapps/test/WEB-INF/web.xml 配置:
...
Tomcat Datasource
jdbc/conMysql
javax.sql.DataSource
Container
------------------------------------------------------------------------------
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"%>
<head>
JSP连接MYSQL数据池<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")%>
<%
}
}
rs.close(); //关闭ResultSet对象
stmt.close(); //关闭Statement对象
conn.close(); //关闭Connection对象
}
catch (Exception e)
{
out.println(e);
}
%>
</body>
错误代码:
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)
自己UP一下!
关注!
你用的是Tomcat吧!?你在common/lib/下放了MYsql的驱动包没有?2个......
驱动有啊 我直接在jsp 文件中连接数据库可以啊.
<%@ page language="java" %>
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*,javax.sql.*"%>
<head>
JSP连接MYSQL数据库<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")%>
<%
}
rs.close(); //关闭ResultSet对象
stmt.close(); //关闭Statement对象
conn.close(); //关闭Connection对象
%>
再顶!
你上面回出现编译错误是因为Connection conn = ds.getConnection();这个申明是在一个判断语句里面的。但是外面你却用到了。conn.close();你要把上面的这个申明放到判断语句外面。
你的Connection conn = ds.getConnection();放在else里啊,要是不执行这句,后面就没conn,那conn.close(); 从何而来
谢谢