Pages

Friday, April 18, 2014

JSTL/DataSources in Tomcat 6.0

1, Install the JDBC drivers in TC's lib/ directory.

    Note: JDBC drivers are usually packaged in jar files. For example, derby*.jar for
    Java DB, ojdbc6.jar for Oracle 11g, and sqljdbc4.jar for SQL Server 2008.
  

2, Add <Resource> in /META-INF/context.xml    Follow the instructions from TC website:
    http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html#JDBC_Data_Sources

    <Resource name="jdbc/jvdb" auth="Container" type="javax.sql.DataSource"
                    maxActive="100" maxIdle="3" maxWait="10000" username="user1" password=""
                    driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
                    url="jdbc:derby:/www/db/jvdb"/>
   </Context>


3, Add resource-ref in /WEB-INF/web.xml
    For example,

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

4, Use the datasource in JSTL(v1.2)
    For example, if you are using Derby, you can query Derby's SYSTABLES in a jsp as follows. Of course, we can always change the SQL to query the user-defined tables.

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>   
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   
  
  <html> 
  <head>
    <SQL:QUERY var="systables" datasource="jdbc/jvdb">   
       select * from sys.SYSTABLES
    </SQL:QUERY>
...

Reference:


2 comments:

  1. Is there a reason why are using {datasource="jdbc/oradb"} in your jsp file, while along you were using jdbc/jvdb?

    I cheked out "http://www.davidghedini.com/pg/entry/tomcat_oracle_jdbc" and that sample code consistently used jdbc/oradb.

    Thanks,

    Daniel

    ReplyDelete
    Replies
    1. Thanks for pointing out it. It's a copy&paste error. I corrected it with jdbc/jvdb datasource ref name and updated the SQL to query a system table in Derby.

      Delete