context.xml中
 <Resource name="jdbc/guestbook"  
auth="Container" 
type="javax.sql.DataSource" 
password="root" 
username="root"  
driverClassName="org.gjt.mm.mysql.Driver"  
url="jdbc:mysql://localhost/test"   
maxActive="10"  
maxIdle="4"  
maxWait="5000"/>获取数据连接:
Context context = new InitialContext()
Datasource ds = context.lookup("java:comp/env/jdbc/guestbook");
Connection ct = ds.getConnection();抛出异常:
javax.naming.NameNotFoundException: Name guestbook is not bound in this Context 
    at ort.apache.naming.namingContext.lookup(NamingContext.java:770)
    at ort.apache.naming.namingContext.lookup(NamingContext.java:140)
    at ort.apache.naming.namingContext.lookup(NamingContext.java:781)
    at ort.apache.naming.namingContext.lookup(NamingContext.java:153)
    at ort.apache.naming.SelectorContext.lookup(NamingContext.java:137)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at com.wh
.guestbook.AddMessageServlet.update(AddMessageServlet.java:112)
 ...........................

解决方案 »

  1.   

    tomcat的版本多少?6.0 和 5.x的区别蛮大的!
    不同版本的server.xml配置有点区别!
    建议,现在server.xml配置通过后再移植到context.xml里。
      

  2.   

    web.xml有配置吗,类似与这样的:
    <resource-ref>
    <description>MySql Connection</description>
    <res-ref-name>jdbc/TestDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>
      

  3.   

    代码:
    <?xml version="1.0" encoding="UTF-8"?>
    <Context>
    <Resource name="jdbc/guestbook" 
                      auth="Container"
                      type="javax.sql.DataSource"
                      password="root"
                      username="root" 
                      driverClassName="org.gjt.mm.mysql.Driver" 
                      url="jdbc:mysql://localhost/test" 
                      maxActive="10" 
                      maxIdle="4" 
                      maxWait="5000"/> 
    </Context>这样对不对!我也遇到过这样的异常,是我写错了<Context></Context>,试试.
      

  4.   

    有没有web应用.xml,否则会报nameexception
      

  5.   

    web.xml中增加了这个还是同样的错误异常。
    <resource-ref>
            <description>MySql Connection</description>
            <res-ref-name>jdbc/guestbook</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
    </resource-ref>
      

  6.   

    server.xml中也要配置的吗?
    是和context.xml中一样的配置吗?
      

  7.   

    我的tomcat是6.0的context.xml中 
    <Resource name="jdbc/guestbook"  
    auth="Container" 
    type="javax.sql.DataSource" 
    password="root" 
    username="root"  
    driverClassName="org.gjt.mm.mysql.Driver"  
    url="jdbc:mysql://localhost/test"  
    maxActive="10"  
    maxIdle="4"  
    maxWait="5000"/> 获取数据连接: 
    Context context = new InitialContext() 
    Datasource ds = context.lookup("java:comp/env/jdbc/guestbook"); 
    Connection ct = ds.getConnection();
     
    web.xml中 
    <resource-ref> 
            <description>MySql Connection </description> 
            <res-ref-name>jdbc/guestbook </res-ref-name> 
            <res-type>javax.sql.DataSource </res-type> 
            <res-auth>Container </res-auth> 
    </resource-ref> 增加了这个还是同样的错误异常。
      

  8.   

     <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
                   maxActive="100" maxIdle="30" maxWait="10000"
                   username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost:3306/test1?autoReconnect=true"/>貌似是你驱动版本太老了换我这个试试
    initContext = new InitialContext();
     Context envContext  = (Context)initContext.lookup("java:/comp/env");
     DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");
     Connection conn = ds.getConnection();
      

  9.   

     <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
                   maxActive="100" maxIdle="30" maxWait="10000"
                   username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost:3306/test1?autoReconnect=true"/>