ITPub博客

首页 > IT职业 > IT职场 > java用代码控制H2 Database启动方法

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

IT职场 作者:jockeyw 时间:2021-09-09 09:12:12 0 删除 编辑

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     
3         
4     

示例代码:


 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     
 3         org.h2.server.web.DbStarter
 4     
 5     
 6         db.url
 7         jdbc:h2:r:/h2db/awbprint/x4z5gjb3
 8     
 9     
10         db.user
11         sa
12     
13     
14         db.password
15         sa
16     
17     
18         db.tcpServer
19         -tcpAllowOthers
20     

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

   
        db.tcpServer
        -tcpAllowOthers
    

去掉即可.

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

 1     
 3         
 4             
 5                 <!-- 文件方式:必须与web.xml中配置的h2db文件名一致 -->
 6                 
 7                 
 8                 
 9             
10         
11     


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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2063/viewspace-2800457/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    73
  • 访问量
    554