首页 > 代码库 > java:如何用代码控制H2 Database启动

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     @Test15     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 h251             Connection conn = DriverManager.getConnection(52                     "jdbc:h2:./h2db/sxaz42b4", "sa", "sa");53 54             Statement stat = conn.createStatement();55 56             // create table57             stat.execute("CREATE TABLE TEST(NAME VARCHAR)");58 59             // insert table60             stat.execute("INSERT INTO TEST VALUES(‘菩提树下的杨过‘)");61             stat.execute("INSERT INTO TEST VALUES(‘http://yjmyzz.cnblogs.com/‘)");62 63             // retrive data64             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 table71             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 }
View Code

输出:

正在启动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>
View Code

示例代码:

技术分享
 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 }
View Code

输出:

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

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

 

java:如何用代码控制H2 Database启动