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>

解决方案 »

  1.   

    错误代码:org.apache.jasper.JasperException: Unable to compile class for JSPAn 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)
      

  2.   

    你用的是Tomcat吧!?你在common\lib\下放了MYsql的驱动包没有?2个......
      

  3.   

    驱动有啊 我直接在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对象 
    %>
      

  4.   

    你上面回出现编译错误是因为Connection conn = ds.getConnection();这个申明是在一个判断语句里面的。但是外面你却用到了。conn.close();你要把上面的这个申明放到判断语句外面。
      

  5.   

    你的Connection conn = ds.getConnection();放在else里啊,要是不执行这句,后面就没conn,那conn.close(); 从何而来