H2 Database Engine
http://www.h2database.com/html/main.html
一個小型的database,可以跟著你的project跑,不用安裝一堆資料庫。
使用步驟:
1. JAR檔:c:\Program Files\H2\bin\h2.jar
2. web.xml設定:
在web.xml加入下面的code,就可以使用http://localhost:8080/console連到它的console了。
<!-- H2-DB的設定 --> <listener> <listener-class>org.h2.server.web.DbStarter</listener-class> </listener> <context-param> <param-name>db.url</param-name> <!-- <param-value>jdbc:h2:dbdata/test</param-value>會出現在eclipse\dbdata下面 --> <!-- <param-value>jdbc:h2:/dbdata/test</param-value>會出現在d:\dbdata下面 --> <param-value>jdbc:h2:/workspace/TWM/H2Sample/dbdata/mycv</param-value> </context-param> <context-param> <param-name>db.user</param-name> <param-value>sa</param-value> </context-param> <context-param> <param-name>db.password</param-name> <param-value>sa</param-value> </context-param> <context-param> <param-name>db.tcpServer</param-name> <param-value>-tcpAllowOthers</param-value> </context-param> <servlet> <servlet-name>H2Console</servlet-name> <servlet-class>org.h2.server.web.WebServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>H2Console</servlet-name> <url-pattern>/console/*</url-pattern> </servlet-mapping>
3. Connection、JdbcDataSource and JdbcConnectionPool:
根據官方的說法,其實是不需用到DataSource的,應為它用JDBC的方式取Connection已經很快了~~
static private String url = "jdbc:h2:/workspace/TWM/H2Sample/dbdata/mycv";
static private String user = "sa";
static private String password = "sa";
static private JdbcDataSource jdbcDataSource = null;
static private JdbcConnectionPool jdbcConnectionPool = null;
static public Connection conn = null;
static {
logger.info("DBUtil init JdbcConnectionPool..");
jdbcDataSource = new JdbcDataSource();
jdbcDataSource.setURL(url);
jdbcDataSource.setUser(user);
jdbcDataSource.setPassword(password);
jdbcConnectionPool = JdbcConnectionPool.create(jdbcDataSource);
logger.info("DBUtil init JdbcConnectionPool success.");
}
/**
* get connection by data source
* @return current connection
* @throws SQLException
*/
static public Connection getConnection() throws Exception{
if(conn == null){
// Class.forName("org.h2.Driver");
// conn = DriverManager.getConnection(url, user, password);
conn = jdbcConnectionPool.getConnection();
}
return conn;
}
4. Hibernate.cfg.xml
<hibernate-configuration> <session-factory> <property name="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.connection.driver_class">org.h2.Driver</property> <property name="hibernate.connection.password">sa</property> <property name="hibernate.connection.url">jdbc:h2:/workspace/TWM/H2Sample/dbdata/mycv</property> <property name="hibernate.connection.username">sa</property> <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property> <mapping resource="per/kirk/mycv/beans/Autobiography.hbm.xml" /> <mapping resource="per/kirk/mycv/beans/Experience.hbm.xml" /> <mapping resource="per/kirk/mycv/beans/Qualification.hbm.xml" /> <mapping resource="per/kirk/mycv/beans/SchoolLevel.hbm.xml" /> <mapping resource="per/kirk/mycv/beans/UserDetail.hbm.xml" /> </session-factory> </hibernate-configuration>
- 若是資料庫不存在,就會動態的建立一個,而帳號密碼就是第一次設定的那個。
- 若URL為
jdbc:h2:~/test,~表示會將資料庫建立在你的HOME下面,像是c:\Documents and Settings\YOUR_NAME\