Thanks for the info.  I have a few questions which are embedded below:

André van Toly wrote:
Hi,

Op 9-nov-2005, om 23:07 heeft Benjamin Slade het volgende geschreven:
Can someone point me to some resources for setting up Tomcat so that servlets/JSP's can connect to a local MySQL database?

The instructions for configuring Connector/J don't seem to match the setup of my tomcat/conf/server.xml file. Specifically, the Connector/J web page talks about adding: Inside the <Context> tag of the conf/server.xml file, but there is no <Context> tag in my conf/server.xml. Should I add one? Or should I add these XML tags to the tomcat/conf/context.xml? Where can I find documentation on the structure of the server.xml and context.xml files? The Apache Tomcat Configuration Reference web page doesn't have much and neither does the Database question in the Tomcat FAQ.
Yes, you need to add a context to your [tomcat]/conf/server.xml. For example for Tomcat's ROOT context the configuration could look like:

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

The ROOT context is the webapp called ROOT :-) which we'll be at http://localhost:8080
For the <Context> tag above:

Using the docBase of "ROOT" is only when placing servlets into a single ROOT directory, right? I'd prefer to set things up with a servlet/JSP in it's own web application directory, because that's how most app's run in real life.

So instead, I'm going to leave out the path="" docBase="ROOT" and I'm going to put the <Context> tag into my $CATALINA_HOME/webapps//MyApps//WEB-INF/web.xml file, near the end (where /MyApps/ is the directory for my test servlet/JSP). Is this correct, will it work? (The answer seems to be "no". See below)

On the other hand, if I wanted to add the above <Context> tag to my server.xml, where do I add it in the file? The web page http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html says:
Add this in between the |</Context>| tag of the examples context and the |</Host>| tag closing the localhost definition. If there is no such tag, you can add one as illustrated in the Context (http://tomcat.apache.org/tomcat-5.5-doc/config/context.html) and Host (http://tomcat.apache.org/tomcat-5.5-doc/config/host.html) configuration references, and repeated below for your convenience.
Both of those web pages are involved architecture reference pages, so unless I hear from someone else on this mailing list, I'll spend the next few hours wading through them.


Your webapp configuration [tomcat]/webapps/ROOT/WEB-INF/web.xml should have a reference to the resource and in this example has to look like this:

  <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/TestDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

Now you can try the example at point 4. of the 'MySQL DBCP Example':
http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html

Remember to install the taglib's the example requires. And you should of course have a MySQL jdbc driver (f.e. mysql-connector-java-3.1.10-bin.jar) installed in [tomcat]/common/lib.
I haven't used taglibs before so I'll have to figure out how to install them.
In the meantime, I tried running the servlet:

<%
       DataSource ds=null;
         Context ctx = new InitialContext();
         if(ctx == null )
             throw new Exception("Boom - No Context");
         ds=(DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
         if(ds == null )
             throw new Exception("Boom - No Datasource");

Connection conn=ds.getConnection();
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select 'Hello World'");
if (rs.next()){
%><%=rs.getString(1)%><%
}
%>
But I get the error "Cannot create JDBC driver of class '' for connect URL 'null'" apparently resulting from the ds.getConnection() call.

Success.

---André The Apache Tomcat Configuration Reference: The Context Container web page seems to have lots of information, but it seems like a lot of reference detail. I just need a database "Hello World" example.

I'm a newbie so I'm not familiar with the structure/semantics of the server.xml and context.xml files. Also, I haven't gotten around to learning how JNDI works. I was sort of hoping to do a "Hello World" database JSP before delving into all of the system guts.

Thanks
Ben Slade


--André van Toly
web http://www.toly.nl                            mobile +31(0)627233562
------------------------------------------------------------------>><<--




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to