Apache Derby Snippets

Here are some quick code snippets to help you get started with embedded Apache Derby.

1. If you use Maven, add the Derby dependency.


2. Connecting to Derby works the same way as other databases. You need the JDBC driver and the database connection URL. Notice create=true in the database URL, which tells Derby to create the database if it does not exist.

	private final static String DB_DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
	private final static String DB_NAME = "sampleDb";
	private final static String DB_URL = String.format("jdbc:derby:%s;create=true", DB_NAME);

3. Calling Class.forName(DB_URL).newInstance(); will start Derby. To shutdown Derby, run the following function.

	private static void shutdownDerby() {
		try {
		} catch (SQLException e) {
			if (((e.getErrorCode() == 50000) && ("XJ015".equals(e.getSQLState())))) {
				// normal shutdown
			} else {
				LOGGER.log(Level.WARNING, e.getMessage(), e);

According to the Apache Derby documentation:

A clean shutdown performs a checkpoint and releases resources. If an embedded application doesn’t shut down Derby, a checkpoint won’t be performed. Nothing bad will happen; it just means that the next connection will be slower because Derby will run its recovery code.

4. To check if a Derby table exists, use DatabaseMetaData.

	public static boolean tableExists(Connection conn, String tableName) {
		boolean tableExists = false;

		ResultSet rs = null;
		try {
			DatabaseMetaData meta = conn.getMetaData();
			rs = meta.getTables(null, null, null, new String[] { "TABLE" });
			while (rs.next()) {
				String currentTableName = rs.getString("TABLE_NAME");
				if (currentTableName.equalsIgnoreCase(tableName)) {
					tableExists = true;
		} catch (SQLException e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
		} finally {
			DbUtils.closeQuietly(rs); // Apache Commons DbUtils

		return tableExists;

Hot Deployment of Java Web Applications in Eclipse

Being able to hot deploy changes you make to your Java web applications is very important. This makes development more productive since it eliminates the build and deploy steps. You use your IDE to make changes and you can see and test your changes right away. Here are ways to do it when using Eclipse.

1. Servers View

The easiest way to hot deploy your Java web application is to create a Server in the Eclipse Servers view and run your web application inside the Server. For this article, We will use Tomcat 7 as our Server.

Servers View

Server View - Figure 2

Your web application must be assigned the Dynamic Web Module Project Facet for this to work. If you didn’t create this web application using the Eclipse New Project Wizard, you will have to assign this Project Facet in Project Properties.

Servers View, Figure 3

Now make changes to your controller and you should see Eclipse automatically reload the web application a few seconds after you save your changes.

Servers View, Figure 4

Servers View, Figure 5

2. Maven Jetty Plugin

If you use Maven to run your server, you can use the Jetty Plugin. It can be configured to periodically scan for changes and automatically redeploy the webapp.

Add the Jetty plugin in pom.xml and run jetty:run. Version 6.1.26 is the latest as of this writing.

project-dir> mvn jetty:run

Now make changes to your controller in Eclipse and you should see the Maven Jetty plugin automatically reload the web application.

3. JRebel

If you have to run Tomcat from the command line or as a service, you can use JRebel. If you need to reload libraries or you have a more complicated application setup, you also will have to use JRebel. There’s a small annual license fee, and it’s free for OSS developers.

To set up, get a JRebel license and install the JRebel Eclipse Plugin from the Eclipse update site http://www.zeroturnaround.com/update-site/. When prompted for a license, copy the license file to the plugin directory and point the license prompt to that file. The plugin directory is something like <eclipse-home>pluginsorg.zeroturnaround.eclipse.embedder_4.*jrebel, and this is where jrebel.jar lives. If you use Pulse, this will be under the Genuitec/Common folder.

To setup the web application, right click on the project and go to JRebel -> Generate rebel.xml in src/main/resources.


To setup Tomcat, create <tomcat-home>/bin/startup-jrebel. cmd and use this to run Tomcat.

@echo off
set JAVA_OPTS=-javaagent:"<jrebel-home>jrebel.jar" %JAVA_OPTS%
call "%~dp0startup.bat" %*

JRebel, Figure 2

Now make changes to your controller in Eclipse and you should see JRebel automatically reload the class.

4. Exploded WAR

Another approach is to deploy an exploded war file, and configure Eclipse to write classes to it. You can write a custom Project Builder to copy the .class files to the exploded war file.

Exploded WAR

If your operating system supports it, you can also create a symbolic link so you don’t have to worry about copying the files. If you don’t want to deploy an exploded war file, just overwrite the .war file every time you make changes in Eclipse using a custom Builder and Tomcat should reload the web application automatically.

Quick Thymeleaf 1.0.0 Tutorial

Here’s a quick way to get you started with Thymeleaf 1.0.0. You will need Eclipse Indigo and the m2e Maven Plugin for Eclipse. You will also need a basic understanding of Spring 3 MVC, as we will convert the Spring MVC view from JSPs to Thymeleaf.

1. Create a new Maven Project using the Eclipse New Wizard.

Maven Project

2. When prompted for the archetype, pick the spring-mvc-jpa-archetype. This will give us a running Spring MVC web application.

Spring MVC web application

3. Enter com.teamextension for the groupId. Enter quickthymeleaf for the artifactId. Enter com.teamextension.quickthymeleaf for the package. Hit Finish.


So far we have created a basic Spring MVC web application. Next step is to add Thymeleaf support.

4. Right click on the quickthymeleaf project and go to Maven -> Add Dependency. Enter the pattern thymeleaf and pick the org.thymeleaf thymeleaf-spring3.
Add Dependency

This adds the Thymeleaf library to our Spring MVC application.

5. Go to src/main/webapp/WEB-INF/spring/app/servlet-context.xml and make the following changes.

Remove the org.springframework.web.servlet.view.InternalResourceViewResolver bean and replace it with

<beans:bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
	<beans:property name="prefix" value="/WEB-INF/views/" />
	<beans:property name="suffix" value=".html" />
	<beans:property name="templateMode" value="HTML5" />

<beans:bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
	<beans:property name="templateResolver" ref="templateResolver" />

<beans:bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
	<beans:property name="templateEngine" ref="templateEngine" />

This changes the ViewResolver from using JSPs to using Thymeleaf.

6. Now we change the JSPs. Go to src/main/webapp//WEB-INF/views. Rename home.jsp to home.html. Replace the 2 JSP directives on the top of home.html.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>

with the HTML5 doctype

<!DOCTYPE html>

Also replace



<p th:text="${controllerMessage}"></p>

Remember, Thymeleaf is not a sequential text processor and instead works of XML based formats. The final home.html should look like this.

<!DOCTYPE html>
	Hello world!
<p th:text="${controllerMessage}"></p>

7. Edit src/main/resources/log4j.xml. Look for “org.application” and change it to “com.teamextension”. This will enable debug logging for our web application.

8. Run mvn tomcat:run to test your web application. Here we will create an Eclipse run configuration for Maven so we can run tomcat:run from within Eclipse.

Run MVN Tomcat

9. Go to http://localhost:8080/quickthymeleaf to see your web application in action.

see your web application

10. For development purposes, you might want to disable caching. Add the cacheable property to the templateResolver bean to control template caching.

<beans:property name=”cacheable” value=”false” />

Now you have a fully configured Spring 3 MVC + Thymeleaf web application. For a more in-depth look at Thymeleaf, please read the official Thymeleaf Documentation.