How to Establish a JDBC Connection in Apache Tomcat (Windows)

See Apache: Tips and Tricks for similar articles.

Business web applications rely on databases. Therefore, you will probably be required to make a connection to your relational database during the development of a web application. Java accesses relational databases through JDBC (Java Database Connectivity). As of JDBC 2, the database is represented to Tomcat as a data source. In this topic I will discuss establishing a JDBC connection to a MySQL data source in Tomcat.

To learn how to establish a JDBC connection in Apache Tomcat, follow these four steps:

  1. I will present the steps using the MySQL database and sample web application we use in our Webucator Tomcat training. The database name is test and is used by the "Cool Garden Tools" web application. It contains one table, the GardenTools table. The JNDI (Java Naming and Directory Interface) name referenced in the web application is jdbc/gardentools.
  2. A resource entry is required in the Tomcat configuration. In my example, I have placed my resource element in CATALINA_BASE/conf/context.xml, e.g., c:/Tomcat8/conf/context.xml:
    <Resource name="jdbc/gardentools" auth="Container" type="javax.sql.DataSource"
                   maxTotal="25" maxIdle="5" maxWaitMillis="5000"
                   username="root" password="mypassword" driverClassName="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost:3306/test"/>  
    	
    The name attribute value must match the name of the JNDI resource coded in the lookup method of a Context in the application code. The maxTotal, maxIdle and maxWaitMillis are parameters for Database Connection Pool (DBCP) and specify the total connections allowed for this resource, the maximum number of idle, or unused, connections and the maximum wait time in milliseconds before throwing an exception if all connections are in use (a value of -1 indicates that the wait time is infinite), respectively. Note that the url and driverClassName are specific to database vendor. For examples of Oracle and PostgreSQL visit the Tomcat documentation for JNDI
  3. Once you save context.xml, Tomcat will detect the updated file, and the resource will be available to your web application.
  4. As an example, here is the Cool Garden Tools welcome page displaying the tools from the GardenTools table: Cool Garden Tools welcome page The table displays the tool ID and description for each tool in the database.

Related Articles

  1. How to Start and Stop Apache Tomcat from the Command Line (Windows)
  2. How to Deploy a WAR File to Apache Tomcat (Windows)
  3. How to Start and Stop Apache Tomcat from the Command Line (Linux)
  4. How to Deploy a WAR File to Apache Tomcat (Linux)
  5. How to Run a JSP Program in Apache Tomcat (Windows)
  6. How to Check the Status of the Apache Tomcat Server (Windows)
  7. How to Deploy a Web Application Using the Apache Tomcat Manager (Windows)
  8. How to Check the Status of the Apache Tomcat Server (Linux)
  9. How to Set Default Context Path in Apache Tomcat (Windows)
  10. How to Use the autoDeploy Attribute in Apache Tomcat (Windows)
  11. How to Deploy a Web Application Using the Apache Tomcat Manager (Linux)
  12. How to List Deployed Applications Using the Apache Tomcat Manager (Windows)
  13. How to Run Multiple Instances of Apache Tomcat on One Server (Windows)
  14. How to Run a JSP Program in Apache Tomcat (Linux)
  15. How to Establish a JDBC Connection in Apache Tomcat (Windows) (this article)
  16. How to Verify Apache Tomcat Server Operation (Windows)
  17. How to Cluster in Apache Tomcat (Linux)
  18. How to Undeploy Web Applications Using the Apache Tomcat Manager (Windows)
  19. How to Set Default Context Path in Apache Tomcat (Linux)
  20. How to Use the Java Logging API in Apache Tomcat (Windows)
  21. How to Cluster in Apache Tomcat (Windows)
  22. How to Use the Java Logging API in Apache Tomcat (Linux)
  23. How to Use the autoDeploy Attribute in Apache Tomcat (Linux)
  24. How to Configure Apache Tomcat to Use MBeans (Windows)
  25. How to List Deployed Applications Using the Apache Tomcat Manager (Linux)
  26. How to Install and Configure Apache Tomcat (Windows)
  27. How to Configure Apache Tomcat to Use MBeans (Linux)