手记

java用代码控制H2 Database启动方法

1、纯手动start/stop

1 package com.cnblogs.yjmyzz.h2;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 
 9 import org.h2.tools.Server;
10 import org.junit.Test;
11 
12 public class H2ServerTest {
13 
14     @Test
15     public void h2Test() {
16         start();
17         crudTest();
18         stop();
19     }
20 
21     private Server server;
22 
23     public void start() {
24         try {
25             System.out.println("正在启动h2...");
26             server = Server.createTcpServer(
27                     new String[] { "-tcp", "-tcpAllowOthers", "-tcpPort",
28                             "8043" }).start();
29             System.out.println("启动成功:" + server.getStatus());
30         } catch (SQLException e) {
31             System.out.println("启动h2出错:" + e.toString());
32 
33             e.printStackTrace();
34             throw new RuntimeException(e);
35         }
36     }
37 
38     public void stop() {
39         if (server != null) {
40             System.out.println("正在关闭h2...");
41             server.stop();
42             System.out.println("关闭成功.");
43         }
44     }
45 
46     public void crudTest() {
47         try {
48             Class.forName("org.h2.Driver");
49 
50             // connect to h2
51             Connection conn = DriverManager.getConnection(
52                     "jdbc:h2:./h2db/sxaz42b4", "sa", "sa");
53 
54             Statement stat = conn.createStatement();
55 
56             // create table
57             stat.execute("CREATE TABLE TEST(NAME VARCHAR)");
58 
59             // insert table
60             stat.execute("INSERT INTO TEST VALUES('菩提树下的杨过')");
61             stat.execute("INSERT INTO TEST VALUES('http://yjmyzz.cnblogs.com/')");
62 
63             // retrive data
64             ResultSet result = stat.executeQuery("select name from test ");
65             int i = 1;
66             while (result.next()) {
67                 System.out.println(i++ + ":" + result.getString("name"));
68             }
69 
70             // drop table
71             stat.execute("DROP TABLE TEST");
72 
73             result.close();
74             stat.close();
75             conn.close();
76         } catch (Exception e) {
77             e.printStackTrace();
78         }
79     }
80 
81 }

输出:

正在启动h2...
启动成功:TCP server running at tcp://192.168.1.100:8043 (others can connect)
1:菩提树下的杨过
2:http://yjmyzz.cnblogs.com/
正在关闭h2...
关闭成功.

 

2、借助Spring

1     <bean id="h2Server" class="org.h2.tools.Server"
2         factory-method="createTcpServer" init-method="start" destroy-method="stop">
3         <constructor-arg value="-tcp,-tcpAllowOthers,-tcpPort,8043" />
4     </bean>

示例代码:


 1 package com.cnblogs.yjmyzz.h2;
 2 
 3 import java.sql.SQLException;
 4 
 5 import org.h2.tools.Server;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.AbstractApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 public class App {
11 
12     public static void main(String[] args) throws SQLException,
13             ClassNotFoundException {
14 
15         ApplicationContext context = new ClassPathXmlApplicationContext(
16                 "spring-context.xml");
17 
18         Server h2Server = context.getBean(Server.class);
19         System.out.println(h2Server.getStatus());
20 
21         H2ServerTest test = new H2ServerTest();
22         test.crudTest();
23 
24         ((AbstractApplicationContext) context).close();
25 
26     }
27 
28 }

输出:

TCP server running at tcp://192.168.1.100:8043 (others can connect)
1:菩提树下的杨过
2:http://yjmyzz.cnblogs.com/

注:用Spring注入的方式,不用刻意手动处理h2Server的start/stop

 

3、随webApp启动时,自动启动h2 server

a) 在web.xml最开头加入下面这段


 1     <!-- h2 -->
 2     <listener>
 3         <listener-class>org.h2.server.web.DbStarter</listener-class>
 4     </listener>
 5     <context-param>
 6         <param-name>db.url</param-name>
 7         <param-value>jdbc:h2:r:/h2db/awbprint/x4z5gjb3</param-value>
 8     </context-param>
 9     <context-param>
10         <param-name>db.user</param-name>
11         <param-value>sa</param-value>
12     </context-param>
13     <context-param>
14         <param-name>db.password</param-name>
15         <param-value>sa</param-value>
16     </context-param>
17     <context-param>
18         <param-name>db.tcpServer</param-name>
19         <param-value>-tcpAllowOthers</param-value>
20     </context-param>

注:具体的文件位置及用户名、密码请自行修改,如果只允许本机连接,把

    <context-param>
        <param-name>db.tcpServer</param-name>
        <param-value>-tcpAllowOthers</param-value>
    </context-param>

去掉即可.

b) 然后在spring配置中参考下面的内容

 1     <bean id="dataSource" class="org.h2.jdbcx.JdbcConnectionPool"
 2         destroy-method="dispose">
 3         <constructor-arg>
 4             <bean class="org.h2.jdbcx.JdbcDataSource">
 5                 <!-- 文件方式:必须与web.xml中配置的h2db文件名一致 -->
 6                 <property name="URL" value="jdbc:h2:r:/h2db/awbprint/x4z5gjb3" />
 7                 <property name="user" value="sa" />
 8                 <property name="password" value="sa" />
 9             </bean>
10         </constructor-arg>
11     </bean>


这样,webapp启动时,会先启动h2 server,后面的代码就能连接到h2了。其它应用也可以用 jdbc:h2:tcp://172.21.129.181/r:/h2db/awbprint/x4z5gjb3 的方式连接到该h2 server (中间加粗的红色IP地址,为web server对应的IP地址)

0人推荐
随时随地看视频
慕课网APP