1. 程式人生 > >windows下創建h2集群,及java集成詳細步驟

windows下創建h2集群,及java集成詳細步驟

arch 需要 sel upd cut exce orm reat 目錄

1.下載h2包,解壓

技術分享

2.cmd操作,進入bin目錄

技術分享

3.創建兩個目錄

技術分享

4.建立集群

輸入以下命令(需要進入h2的bin目錄)

java -cp "h2-1.4.195.jar;%H2DRIVERS%;%CLASSPATH%" org.h2.tools.Server -tcpAllowOthers -tcpPort 9101 -webAllowOthers -webPort 8081 -baseDir D:\database\h2Cluter\h2_1

java -cp "h2-1.4.195.jar;%H2DRIVERS%;%CLASSPATH%" org.h2.tools.Server -tcpAllowOthers -tcpPort 9102 -webAllowOthers -webPort 8082 -baseDir D:\database\h2Cluter\h2_2

java -cp h2-1.4.195.jar org.h2.tools.CreateCluster -urlSource "jdbc:h2:tcp://127.0.0.1:9101/testCluster" -urlTarget "jdbc:h2:tcp://127.0.0.1:9102/testCluster" -user "sa" -serverList "127.0.0.1:9101,127.0.0.1:9102"

技術分享

5.瀏覽器查看集群

http://192.168.30.50:8081

或者http://192.168.30.50:8082

技術分享

技術分享

6.java連接

導入jar包

public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String sourceURL 
= "jdbc:h2:tcp://localhost:9101,localhost:9102/testCluster";// H2DB mem // mode String user = "sa"; String key = ""; Class.forName("org.h2.Driver");// HSQLDB Driver Connection conn = DriverManager.getConnection(sourceURL, user, key);//
把驅動放入連接 Statement stmt = conn.createStatement(); stmt.execute("CREATE TABLE mytable(name VARCHAR(100),sex VARCHAR(10))"); // String sql = "SELECT VALUE FROM INFORMATION_SCHEMA.SETTINGS WHERE // NAME=‘CLUSTER‘"; stmt.executeUpdate("INSERT INTO mytable VALUES(‘Steven Stander‘,‘male‘)"); stmt.executeUpdate("INSERT INTO mytable VALUES(‘Elizabeth Eames‘,‘female‘)"); stmt.executeUpdate("DELETE FROM mytable WHERE sex=‘male‘"); stmt.close(); conn.close(); }

windows下創建h2集群,及java集成詳細步驟