java:如何用代碼控制H2 Database啟動

來源:互聯網
上載者:User

標籤:

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啟動

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.