HSQLDB

package com.hsqldb.dbfilter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.hsqldb.Server;


public class DBRunListener implements ServletContextListener{
	
	private static Server server;
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		new Thread(){
			public void run() {
				server.shutdown();
			}
		}.start();
	}
	@Override
	public void contextInitialized(ServletContextEvent config) {
		String filepath = config.getServletContext().getRealPath(config.getServletContext().getInitParameter("DBpropertyFile"));
		Properties p = new Properties();
		try {
			p.load(new FileInputStream(filepath));
		} catch (FileNotFoundException e) {
			System.out.println(filepath +" not found");
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String dbpath = p.getProperty("dbpath");
		String dbname = p.getProperty("dbname");
		String dbport = p.getProperty("dbport");
		
		server = new Server();
		server.setDatabasePath(0, dbpath);
		server.setDatabaseName(0, dbname);
		server.setPort(Integer.valueOf(dbport));
		//设置控制台不打印输出
		//server.setSilent(true);
		//server.setTrace(false);
		
		new  Thread() {
			public void run() {
				server.start();
			}
		}.start();
	}
}

 

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>HSQLDB</display-name>

	<listener>

	</listener>
	<listener>
		<!-- 初始化LOG4J  -->
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
		<!-- 启动HSQLDB数据库 -->
		<listener-class>com.hsqldb.dbfilter.DBRunListener</listener-class>
		<!-- 加载Spring容器配置 -->
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 设置数据库配置文件路径 -->
	<context-param>
		<param-name>DBpropertyFile</param-name>
		<param-value>WEB-INF/db.properties</param-value>
	</context-param>

	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/log4j.properties</param-value>
	</context-param>

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 WEB-INF/db.properties;

 

dbpath=D:\workspace\HSQLDB\WebContent\WEB-INF\lib
dbname=mydb
dbport=9999

driver=org.hsqldb.jdbcDriver
url=jdbc:hsqldb:hsql://localhost:9999/mydb
username=sa
password=
 

 

你可能感兴趣的:(HSQLDB)