Newer
Older
jetty / src / main / java / dao / DbConnection.java
package dao;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import org.h2.jdbcx.JdbcConnectionPool;

import javax.servlet.ServletContext;

public class DbConnection {

	private static final String USERNAME = "sa";
	private static final String PASSWORD = "";

	private static JdbcConnectionPool pool;

	public static Connection getConnection(ServletContext ctx) {

		String path = ctx.getRealPath(File.separator);
		String uri = "jdbc:h2:file:"+path+"/../db/users;AUTO_SERVER=TRUE;IFEXISTS=true";

		System.out.println(uri);

		if (pool == null) {
			pool = JdbcConnectionPool.create(uri, USERNAME, PASSWORD);
		}

		try {
			return pool.getConnection();
		} catch (SQLException ex) {
			throw new RuntimeException(ex);
		}
	}

}