You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by David McMinn <d_...@yahoo.com> on 2006/02/24 09:21:27 UTC

First JDBC tomcat app

I'm stepping through the example Professional Apache Tomcat 5 book by Wrox in Chapter 14 - I have set up the mysql database and confirmed it works and set up the tomcat server and confirmed that I can see my index.jsp page. When I try to go to http://localhost:8070/jsp-examples/wroxjdbc/JDBCTest.jsp
  I get a standard The page cannot be displayed page.
   
  I've included all my steps below. Anyone that can help is most appreciated. Thanks in advance.....Dave
   
  Steps I have done
   
  1) Created a DB called everycitizen and a table called test with a column called pk. Created user everyuser w/ a password and granted Select privileges to that user.
  2) Copied the mysql-connector-java-3.1.12-bin.jar into $CATALINA_HOME/common/lib.
  3)Added the following to the $CATALINA_HOME/conf/server.xml just before the </Host tag>. Password is blotted out.
  
 <!-- added by DM 2/22/2006 -->
 <DefaultContext>
  <Resource name="jdbc/WroxTC5" auth="Container" type="javax.sql.Datasource"
  driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/everycitizen"
  username="everyuser" password="everypass" maxActive="20" maxIdle="30000" maxWait="100"/>
 </DefaultContext>
  4) Added the following to the $CATALINA_HOME/webapps/jsp-examples/WEB-INF web.xml file at the bottom just before the </web-app> entry after the last env-entry. 
  
    <resource-ref>
      <res-ref-name>jdbc/WroxTC5</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
    </resource-ref>

  5) Added the JDBCTest.jsp file and the errorpg.jsp file to $CATALINA_HOME/webapps/jsp-examples/wroxjdbc directory. I created the wroxjdbc folder. The JDBC Test is:
   
  <html>
 <head>
  <%@ page errorPage="errorpg.jsp"
   import="java.sql.*,
     javax.sql.*,
     java.io.*,
     javax.naming.InitialContext,
     javax.naming.Context" %>
 </head>
   <h1>JDBC JNDI Resource Test</h1>
  <%
  InitialContext initCtx = new InitialContext();
  DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/WroxTC5");
  Connection conn = ds.getConnection();
  Statement stmt = conn.createStatement();
  ResultSet rset = stmt.executeQuery("select * from test;");
  %>
  <table width = '600' border='1'>
   <tr>
    <th align='left'>XXXX</th>
   </tr>
   <%
   while (rset.next())
   {
   %>
    <tr><td> <%=rset.getInt(0)%></td></tr>
   <% } 
   rset.close();
   stmt.close();
   conn.close();
   initCtx.close();
   %>
  </table>
 </html>
   
   
  and the errorpg is:
   
  <html>
   <%@ page isErrorPage="true" %>
  <h1> An error has occurred </h1>
  <%= exception.getMessage() %>
 </html>

Re: First JDBC tomcat app

Posted by Robert Taylor <64...@bellsouth.net>.
What do the logs say?
Have you tried connecting to MySQL outside of Tomcat?
Does that work?
Can you get the page to display without the JDBC stuff in it?

/robert

David McMinn wrote:
> I'm stepping through the example Professional Apache Tomcat 5 book by Wrox in Chapter 14 - I have set up the mysql database and confirmed it works and set up the tomcat server and confirmed that I can see my index.jsp page. When I try to go to http://localhost:8070/jsp-examples/wroxjdbc/JDBCTest.jsp
>   I get a standard The page cannot be displayed page.
>    
>   I've included all my steps below. Anyone that can help is most appreciated. Thanks in advance.....Dave
>    
>   Steps I have done
>    
>   1) Created a DB called everycitizen and a table called test with a column called pk. Created user everyuser w/ a password and granted Select privileges to that user.
>   2) Copied the mysql-connector-java-3.1.12-bin.jar into $CATALINA_HOME/common/lib.
>   3)Added the following to the $CATALINA_HOME/conf/server.xml just before the </Host tag>. Password is blotted out.
>   
>  <!-- added by DM 2/22/2006 -->
>  <DefaultContext>
>   <Resource name="jdbc/WroxTC5" auth="Container" type="javax.sql.Datasource"
>   driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/everycitizen"
>   username="everyuser" password="everypass" maxActive="20" maxIdle="30000" maxWait="100"/>
>  </DefaultContext>
>   4) Added the following to the $CATALINA_HOME/webapps/jsp-examples/WEB-INF web.xml file at the bottom just before the </web-app> entry after the last env-entry. 
>   
>     <resource-ref>
>       <res-ref-name>jdbc/WroxTC5</res-ref-name>
>       <res-type>javax.sql.DataSource</res-type>
>       <res-auth>Container</res-auth>
>     </resource-ref>
> 
>   5) Added the JDBCTest.jsp file and the errorpg.jsp file to $CATALINA_HOME/webapps/jsp-examples/wroxjdbc directory. I created the wroxjdbc folder. The JDBC Test is:
>    
>   <html>
>  <head>
>   <%@ page errorPage="errorpg.jsp"
>    import="java.sql.*,
>      javax.sql.*,
>      java.io.*,
>      javax.naming.InitialContext,
>      javax.naming.Context" %>
>  </head>
>    <h1>JDBC JNDI Resource Test</h1>
>   <%
>   InitialContext initCtx = new InitialContext();
>   DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/WroxTC5");
>   Connection conn = ds.getConnection();
>   Statement stmt = conn.createStatement();
>   ResultSet rset = stmt.executeQuery("select * from test;");
>   %>
>   <table width = '600' border='1'>
>    <tr>
>     <th align='left'>XXXX</th>
>    </tr>
>    <%
>    while (rset.next())
>    {
>    %>
>     <tr><td> <%=rset.getInt(0)%></td></tr>
>    <% } 
>    rset.close();
>    stmt.close();
>    conn.close();
>    initCtx.close();
>    %>
>   </table>
>  </html>
>    
>    
>   and the errorpg is:
>    
>   <html>
>    <%@ page isErrorPage="true" %>
>   <h1> An error has occurred </h1>
>   <%= exception.getMessage() %>
>  </html>
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org