remm 2004/02/29 04:47:32 Modified: webapps/docs jndi-datasource-examples-howto.xml Log: - Improvements to DataSource HOWTO. - Bug 27307, submitted by Felipe Leme. Revision Changes Path 1.7 +35 -68 jakarta-tomcat-catalina/webapps/docs/jndi-datasource-examples-howto.xml Index: jndi-datasource-examples-howto.xml =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/webapps/docs/jndi-datasource-examples-howto.xml,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- jndi-datasource-examples-howto.xml 15 Nov 2003 09:55:40 -0000 1.6 +++ jndi-datasource-examples-howto.xml 29 Feb 2004 12:47:32 -0000 1.7 @@ -146,15 +146,16 @@ <subsection name="MySQL DBCP Example"> <h3>0. Introduction</h3> -<p>Versions of MySQL and the mm.mysql JDBC driver when have been -reported to work: +<p>Versions of <a href="http://www.mysql.com/products/mysql/index.html">MySQL</a> and JDBC drivers that have been reported to work: <ul> -<li>MySQL 3.23.47, MySQL 3.23.47 using InnoDB, MySQL 4.0.1alpha</li> -<li>mm.mysql 2.0.14 (JDBC Driver)</li> +<li>MySQL 3.23.47, MySQL 3.23.47 using InnoDB,, MySQL 3.23.58, MySQL 4.0.1alpha</li> +<li><a href="http://www.mysql.com/products/connector-j">Connector/J</a> 3.0.11-stable (the official JDBC Driver)</li> +<li><a href="http://mmmysql.sourceforge.net">mm.mysql</a> 2.0.14 (an old 3rd party JDBC Driver)</li> </ul> -Please let us know if you have tested the new MySQL mm.mysql 3.0 driver. </p> +<p>Before you proceed, don't forget to copy the JDBC Driver's jar into <code>$CATALINA_HOME/common/lib</code>.</p> + <h3>1. MySQL configuration</h3> <p> Ensure that you follow these instructions as variations can cause problems. @@ -254,12 +255,20 @@ <value>javadude</value> </parameter> - <!-- Class name for mm.mysql JDBC driver --> + <!-- Class name for the old mm.mysql JDBC driver - uncomment this entry and comment next + if you want to use this driver - we recommend using Connector/J though <parameter> <name>driverClassName</name> <value>org.gjt.mm.mysql.Driver</value> </parameter> - + --> + + <!-- Class name for the official MySQL Connector/J driver --> + <parameter> + <name>driverClassName</name> + <value>com.mysql.jdbc.Driver</value> + </parameter> + <!-- The JDBC connection url for connecting to your MySQL dB. The autoReconnect=true argument to the url makes sure that the mm.mysql JDBC Driver will automatically reconnect if mysqld closed the @@ -278,10 +287,11 @@ <p>Now create a <code>WEB-INF/web.xml</code> for this test application. <source> -<?xml version="1.0" encoding="ISO-8859-1"?> - <!DOCTYPE web-app PUBLIC - "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" - "http://java.sun.com/dtd/web-app_2_3.dtd"> +<web-app xmlns="http://java.sun.com/xml/ns/j2ee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee +http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" + version="2.4"> <web-app> <description>MySQL Test App</description> <resource-ref> @@ -295,78 +305,35 @@ </p> <h3>4. Test code</h3> -<p>Now create a simple test.jsp for use later. +<p>Now create a simple <code>test.jsp</code> page for use later. <source> +<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> + +<sql:query var="rs" dataSource="jdbc/TestDB"> +select id, foo, bar from testdata +</sql:query> + <html> <head> <title>DB Test</title> </head> <body> - <% - foo.DBTest tst = new foo.DBTest(); - tst.init(); - %> - <h2>Results</h2> - Foo <%= tst.getFoo() %><br/> - Bar <%= tst.getBar() %> + +<c:forEach var="row" items="${rs.rows}"> + Foo ${row.foo}<br/> + Bar ${row.bar}<br/> +</c:forEach> </body> </html> </source> </p> -<p>And create a Java class to actually use your new Datasource and connection -pool. Note: this code isn't anywhere near production ready - it's only -supposed to be used as a simple test :-) -<source> -package foo; - -import javax.naming.*; -import javax.sql.*; -import java.sql.*; +<p>That JSP page makes use of <a href="http://java.sun.com/products/jsp/jstl">JSTL</a>'s SQL and Core taglibs. You can get it from Sun's <a href="http://java.sun.com/webservices/downloads/webservicespack.html">Java Web Services Developer Pack</a> or <a href="http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html">Jakarta Taglib Standard 1.1</a> project - just make sure you get a 1.1.x release. Once you have JSTL, copy <code>jstl.jar</code> and <code>standard.jar</code> to your web app's <code>WEB-INF/lib</code> directory. -public class DBTest { - - String foo = "Not Connected"; - int bar = -1; - - public void init() { - try{ - Context ctx = new InitialContext(); - if(ctx == null ) - throw new Exception("Boom - No Context"); - - DataSource ds = - (DataSource)ctx.lookup( - "java:comp/env/jdbc/TestDB"); - - if (ds != null) { - Connection conn = ds.getConnection(); - - if(conn != null) { - foo = "Got Connection "+conn.toString(); - Statement stmt = conn.createStatement(); - ResultSet rst = - stmt.executeQuery( - "select id, foo, bar from testdata"); - if(rst.next()) { - foo=rst.getString(2); - bar=rst.getInt(3); - } - conn.close(); - } - } - }catch(Exception e) { - e.printStackTrace(); - } - } - - public String getFoo() { return foo; } - public int getBar() { return bar;} -} -</source> </p> <p>Finally deploy your web app into <code>$CATALINA_HOME/webapps</code> either
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]