You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Jacquet, Frederic" <FJ...@fciconnect.com> on 2002/02/15 17:03:23 UTC

Datasources context ?

I have got this error
	Name java:comp is not bound in this Context 

When I try the following code :
         try {
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDb");
            Connection conn = ds.getConnection();

            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT prenom,nom FROM
annuaire;");
            while (rs.next()) {
                out.println("<p>" + rs.getString("prenom"));
                out.println("&nbsp;" + rs.getString("nom"));
            }

            conn.close();
        } catch (Exception e) {
            out.print(e.getMessage());
        }
And the declarations 
server.xml
	    <Resource name="jdbc/TestDb" auth="CONTAINER"
                    type="javax.sql.DataSource"/>
          <ResourceParams name="jdbc/TestDb">
            <parameter><name>user</name><value>user</value></parameter>
 
<parameter><name>password</name><value>passwd</value></parameter>
            <parameter><name>driverClassName</name>
              <value>org.gjt.mm.mysql.Driver</value></parameter>
            <parameter><name>driverName</name>
              <value>jdbc:mysql://localhost/test</value></parameter>
          </ResourceParams>
web.xml
<resource-ref>
    <description>Test database.</description>
    <res-ref-name>jdbc/TestDb</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>CONTAINER</res-auth>
</resource-ref>


What is wrong ???

Regards / Cordialement

Frederic Jacquet


Re: Datasources context ?

Posted by Remy Maucherat <re...@apache.org>.
> I have got this error
> Name java:comp is not bound in this Context
>
> When I try the following code :
>          try {
>             Context initCtx = new InitialContext();
>             Context envCtx = (Context) initCtx.lookup("java:comp/env");
>             DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDb");
>             Connection conn = ds.getConnection();
>
>             Statement st = conn.createStatement();
>             ResultSet rs = st.executeQuery("SELECT prenom,nom FROM
> annuaire;");
>             while (rs.next()) {
>                 out.println("<p>" + rs.getString("prenom"));
>                 out.println("&nbsp;" + rs.getString("nom"));
>             }
>
>             conn.close();
>         } catch (Exception e) {
>             out.print(e.getMessage());
>         }

Well, the big questions are:
- where is that code fragment located ?
- are you modifying the context classloader ?

To end up with the right JNDI context, Catalina uses context classloader
associations. So if you get the error you're getting, it means that the CL
association is not the one it should be. There were a few bugs caused
because in some places, it wasn't set properly (for example, you can look at
bug 5330).

Remy


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Datasources context ?

Posted by Frederic Monclar <fr...@profium.com>.
May be you have defined at a wrong place your resource in your
server.xml file.
Fred.


> "Jacquet, Frederic" wrote:
> 
> I have got this error
>         Name java:comp is not bound in this Context
> 
> When I try the following code :
>          try {
>             Context initCtx = new InitialContext();
>             Context envCtx = (Context)
> initCtx.lookup("java:comp/env");
>             DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDb");
> 
>             Connection conn = ds.getConnection();
> 
>             Statement st = conn.createStatement();
>             ResultSet rs = st.executeQuery("SELECT prenom,nom FROM
> annuaire;");
>             while (rs.next()) {
>                 out.println("<p>" + rs.getString("prenom"));
>                 out.println("&nbsp;" + rs.getString("nom"));
>             }
> 
>             conn.close();
>         } catch (Exception e) {
>             out.print(e.getMessage());
>         }
> And the declarations
> server.xml
>             <Resource name="jdbc/TestDb" auth="CONTAINER"
>                     type="javax.sql.DataSource"/>
>           <ResourceParams name="jdbc/TestDb">
> 
> <parameter><name>user</name><value>user</value></parameter>
> 
> <parameter><name>password</name><value>passwd</value></parameter>
>             <parameter><name>driverClassName</name>
>               <value>org.gjt.mm.mysql.Driver</value></parameter>
>             <parameter><name>driverName</name>
>               <value>jdbc:mysql://localhost/test</value></parameter>
>           </ResourceParams>
> web.xml
> <resource-ref>
>     <description>Test database.</description>
>     <res-ref-name>jdbc/TestDb</res-ref-name>
>     <res-type>javax.sql.DataSource</res-type>
>     <res-auth>CONTAINER</res-auth>
> </resource-ref>
> 
> What is wrong ???
> 
> Regards / Cordialement
> 
> Frederic Jacquet
> 
>     ---------------------------------------------------------------
> --
> To unsubscribe:   <ma...@jakarta.apache.org>
> For additional commands: <ma...@jakarta.apache.org>
> Troubles with the list: <ma...@jakarta.apache.org>

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>