1, Pure manual start/stop
1 Packagecom.cnblogs.yjmyzz.h2;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 ImportJava.sql.ResultSet;6 Importjava.sql.SQLException;7 Importjava.sql.Statement;8 9 ImportOrg.h2.tools.Server;Ten Importorg.junit.Test; One A Public classH2servertest { - - @Test the Public voidh2test () { - start (); - crudtest (); - stop (); + } - + Privateserver server; A at Public voidstart () { - Try { -SYSTEM.OUT.PRINTLN ("Starting H2 ..."); -Server =Server.createtcpserver ( - NewString[] {"-tcp", "-tcpallowothers", "-tcpport", -"8043"}). Start (); inSystem.out.println ("Start success:" +server.getstatus ()); -}Catch(SQLException e) { toSYSTEM.OUT.PRINTLN ("Startup H2 error:" +e.tostring ()); + - e.printstacktrace (); the Throw NewRuntimeException (e); * } $ }Panax Notoginseng - Public voidStop () { the if(Server! =NULL) { +System.out.println ("Closing H2 ..."); A server.stop (); theSystem.out.println ("closed successfully.")); + } - } $ $ Public voidcrudtest () { - Try { -Class.forName ("Org.h2.Driver"); the - //Connect to H2WuyiConnection conn =Drivermanager.getconnection ( the"Jdbc:h2:./h2db/sxaz42b4", "sa", "sa"); - WuStatement stat =conn.createstatement (); - About //CREATE TABLE $Stat.execute ("CREATE TABLE TEST (NAME VARCHAR)"); - - //Insert Table -Stat.execute ("INSERT into TEST VALUES (' Yang over ' under the Bodhi Tree ')"); AStat.execute ("INSERT into TEST VALUES (' http://yjmyzz.cnblogs.com/')"); + the //retrive Data -ResultSet result = Stat.executequery ("Select name from Test"); $ inti = 1; the while(Result.next ()) { theSystem.out.println (i++ + ":" + result.getstring ("name"))); the } the - //drop table inStat.execute ("DROP TABLE TEST"); the the result.close (); About stat.close (); the conn.close (); the}Catch(Exception e) { the e.printstacktrace (); + } - } the Bayi}View Code
Output:
Starting H2 ...
Successful start: TCP server running at tcp://192.168.1.100:8043 (others can connect)
1: Poplar under the Banyan tree
2:http://yjmyzz.cnblogs.com/
Shutting down H2 ...
Shutdown succeeded.
2. With spring
1 <BeanID= "H2server"class= "Org.h2.tools.Server"2 Factory-method= "Createtcpserver"Init-method= "Start"Destroy-method= "Stop">3 <Constructor-argvalue= "-tcp,-tcpallowothers,-tcpport,8043" />4 </Bean>
View Code
Example code:
1 Packagecom.cnblogs.yjmyzz.h2;2 3 Importjava.sql.SQLException;4 5 ImportOrg.h2.tools.Server;6 ImportOrg.springframework.context.ApplicationContext;7 ImportOrg.springframework.context.support.AbstractApplicationContext;8 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;9 Ten Public classApp { One A Public Static voidMain (string[] args)throwsSQLException, - ClassNotFoundException { - theApplicationContext context =NewClasspathxmlapplicationcontext ( -"Spring-context.xml"); - -Server H2server = Context.getbean (server.class); + System.out.println (H2server.getstatus ()); - +H2servertest test =Newh2servertest (); A test.crudtest (); at - ( (Abstractapplicationcontext) context). Close (); - - } - -}View Code
Output:
TCP server running at tcp://192.168.1.100:8043 (others can connect)
1: Poplar under the Banyan tree
2:http://yjmyzz.cnblogs.com/
Note: With spring injection, do not deliberately manually handle the H2server start/stop
Java: How to control H2 database startup with code