Name jdbc is not bound in this Context 求助
tomcat的conf\catalina\localhost配置samples.xml<Contextpath="/samples"docBase="samples"de...
tomcat的conf\catalina\localhost配置samples.xml
<Context path="/samples" docBase="samples" debug="0">
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb?characterEncoding=GB2312"
username="root"
password="root"
maxActive="200"
maxIdle="50"
maxWait="3000"/>
</Context>
eclipse的samples工程里名为ConnPool的servlet
package servlet;
import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ConnPool
*/
public class ConnPool extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
response.setCharacterEncoding("GB2312");
PrintWriter out=response.getWriter();
try
{
javax.naming.Context ctx=new javax.naming.InitialContext();
javax.sql.DataSource ds=(javax.sql.DataSource) ctx.lookup("java:/comp/env/jdbc/jdbcdemo");
Connection conn=ds.getConnection();
PreparedStatement pstmt=conn.prepareStatement("select * from t_books");
ResultSet rs=pstmt.executeQuery();
StringBuilder table=new StringBuilder();
table.append("<table border='1'>");
table.append("<tr><td>书名</td><td>作者</td></tr>");
while(rs.next())
{
table.append("<tr><td>"+rs.getString("name")+"</td><td>");
table.append(rs.getString("author")+"</td></tr>");
}
table.append("</table>");
out.println(table.toString());
pstmt.close();
}
catch(Exception e)
{
out.println(e.getMessage());
}
out.flush();
out.close();
}
}.
为什么会这样呢? 展开
<Context path="/samples" docBase="samples" debug="0">
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb?characterEncoding=GB2312"
username="root"
password="root"
maxActive="200"
maxIdle="50"
maxWait="3000"/>
</Context>
eclipse的samples工程里名为ConnPool的servlet
package servlet;
import java.sql.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ConnPool
*/
public class ConnPool extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
response.setCharacterEncoding("GB2312");
PrintWriter out=response.getWriter();
try
{
javax.naming.Context ctx=new javax.naming.InitialContext();
javax.sql.DataSource ds=(javax.sql.DataSource) ctx.lookup("java:/comp/env/jdbc/jdbcdemo");
Connection conn=ds.getConnection();
PreparedStatement pstmt=conn.prepareStatement("select * from t_books");
ResultSet rs=pstmt.executeQuery();
StringBuilder table=new StringBuilder();
table.append("<table border='1'>");
table.append("<tr><td>书名</td><td>作者</td></tr>");
while(rs.next())
{
table.append("<tr><td>"+rs.getString("name")+"</td><td>");
table.append(rs.getString("author")+"</td></tr>");
}
table.append("</table>");
out.println(table.toString());
pstmt.close();
}
catch(Exception e)
{
out.println(e.getMessage());
}
out.flush();
out.close();
}
}.
为什么会这样呢? 展开
1个回答
展开全部
在web.xml中配置如下:
<resource-ref>
<res-ref-name>数据库名</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
在tomcat中配置如下:
<Context path="/web的根目录名">
<Resource name="数据库名"
type="javax.sql.DataSource"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="10"
url="jdbc:mysql://localhost:3306/tarena"
maxActive="10"/>
</Context>
<resource-ref>
<res-ref-name>数据库名</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
在tomcat中配置如下:
<Context path="/web的根目录名">
<Resource name="数据库名"
type="javax.sql.DataSource"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="10"
url="jdbc:mysql://localhost:3306/tarena"
maxActive="10"/>
</Context>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询