setup maria db jdbc resources in tomcat 9

This page describes how to setup Maria DB JDBC Resources in Tomcat 9 and how to create a servlet based webapp without a web.xml.

Full downloadable source for this page is available here. Corrections and enhancements are welcome, fork, change and push back to GitHub.

Database Drivers

Place the database driver jar file into the server/lib folder.

Define the resource in Tomcat

Place the following into the $CATALINA_BASE/conf/context.xml

  <Resource name="jdbc/batch" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="username" password="password" driverClassName="org.mariadb.jdbc.Driver"

Please note that if you are running in Eclipse and have Tomcat setup as a WTP project then this file is located in the “Server” project in your eclipse workspace.

Test using Snoop JSP

To test the datasource simply create a snoop jsp to perform a lookup.

Test using your own Servlet


package com.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

@WebServlet(urlPatterns = "/testServlet", loadOnStartup = 1)
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// You may use the following lines to initialize the context.
		// Obtain our environment naming context
		Context initCtx;
		try {
			initCtx = new InitialContext();
			Context envCtx = (Context) initCtx.lookup("java:comp/env");

			// A data source can be obtained by doing the following.
			// Look up our data source
			DataSource ds = (DataSource) envCtx.lookup("jdbc/test");

			// Allocate and use a connection from the pool
			// ... use this connection to access the database ...
			try (PrintWriter writer = resp.getWriter(); Connection conn = ds.getConnection();) {
				writer.println("connected: " + !conn.isClosed());
				DatabaseMetaData metadata = conn.getMetaData();
				writer.println("Database Product Name: " + metadata.getDatabaseProductName());
				writer.println("Database Product Version: " + metadata.getDatabaseProductVersion());
				writer.println("Logged User: " + metadata.getUserName());
				writer.println("JDBC Driver: " + metadata.getDriverName());
				writer.println("Driver Version: " + metadata.getDriverVersion());
		} catch (Exception e) {

If you are using the popular spring framework you can do the following

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/test"/>
    <property name="resourceRef" value="true"/>

Restart and visit http://localhost:8080/test-jndi/testServlet to view the results of the connection.

Full downloadable source for this page is available here.

0 Responses to “setup maria db jdbc resources in tomcat 9”

  1. Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

March 2017
« Mar    

Blog Stats

  • 836,405 hits

%d bloggers like this: