標籤:
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啟動