JSF example with H2 in memory DB

Looks easy.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>biz.tugay</groupId>
    <artifactId>hello-windows</artifactId>
    <packaging>war</packaging>
 
    <version>1.0-SNAPSHOT</version>
 
    <name>hello-windows Maven Webapp</name>
    <url>http://maven.apache.org</url>
 
    <dependencies>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>javax.faces-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.13</version>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.192</version>
        </dependency>
    </dependencies>
 
    <build>
        <finalName>hello-windows</finalName>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.1.v20140609</version>
                <configuration>
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
 
    <servlet>
        <servlet-name>faces-servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>faces-servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
 
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
 
</web-app>

BackingBean.java
package biz.tugay;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
/**
 * Koray Tugay
 * 6/19/2016
 */
 
@ManagedBean
@RequestScoped
public class BackingBean {
 
    public int insertRandomNumbertoDB() throws ClassNotFoundException, SQLException {
        final Random r = new Random();
        int i = r.nextInt();
        Class.forName("org.h2.Driver");
        final Connection conn = DriverManager.getConnection("jdbc:h2:~/hellowindows",
                "hellowindows", "hellowindows");
        Statement statement = conn.createStatement();
        String sql = "INSERT INTO NUMBERS VALUES(" + i + ")";
        statement.execute(sql);
        conn.close();
        return i;
    }
 
    public List<Integer> randomNumbersInDB() throws ClassNotFoundException, SQLException {
        final List<Integer> integers = new ArrayList<Integer>();
        Class.forName("org.h2.Driver");
        final Connection conn = DriverManager.getConnection("jdbc:h2:~/hellowindows",
                "hellowindows", "hellowindows");
        final Statement statement = conn.createStatement();
        final String sql = "SELECT id FROM NUMBERS";
 
        ResultSet resultSet = statement.executeQuery(sql);
 
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            integers.add(id);
        }
 
        conn.close();
        return integers;
    }
 
}

index.xhtml
<!DOCTYPE html>
<html xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
<head>
    <title>Hello World!!!</title>
</head>
<body>
<h:form>
    <h:commandButton action="${backingBean.insertRandomNumbertoDB()}"
                     value="Insert Random Number to DB"/>
</h:form>
 
<h:dataTable value="${backingBean.randomNumbersInDB()}"
             var="randomNumber">
    <h:column>
        <h:outputText value="${randomNumber}"/>
    </h:column>
</h:dataTable>
</body>
</html>

And I will see output like this:
The DB files are actually created in my Home folder with the name: hellowindows.mv by H2 database itself.

Please note: You must first execute the CREATE TABLE NUMBERS ... H2 db will create the database itself but not the table!