h2 database

介绍

h2-database java版本的数据库

1
2
3
4
5
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>

help说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[lixl@localhost h2db]$ java -cp h2*.jar org.h2.tools.Server -?
Starts the H2 Console (web-) server, TCP, and PG server.
Usage: java org.h2.tools.Server <options>
When running without options, -tcp, -web, -browser and -pg are started.
Options are case sensitive. Supported options are:
[-help] or [-?] Print the list of options
[-web] Start the web server with the H2 Console
[-webAllowOthers] Allow other computers to connect - see below http访问,默认只能本机使用
[-webDaemon] Use a daemon thread
[-webPort <port>] The port (default: 8082) http访问端口号
[-webSSL] Use encrypted (HTTPS) connections
[-browser] Start a browser connecting to the web server
[-tcp] Start the TCP server
[-tcpAllowOthers] Allow other computers to connect - see below tcp访问,默认只能本机使用
[-tcpDaemon] Use a daemon thread
[-tcpPort <port>] The port (default: 9092)
[-tcpSSL] Use encrypted (SSL) connections
[-tcpPassword <pwd>] The password for shutting down a TCP server
[-tcpShutdown "<url>"] Stop the TCP server; example: tcp://localhost
[-tcpShutdownForce] Do not wait until all connections are closed
[-pg] Start the PG server
[-pgAllowOthers] Allow other computers to connect - see below
[-pgDaemon] Use a daemon thread
[-pgPort <port>] The port (default: 5435)
[-properties "<dir>"] Server properties (default: ~, disable: null)
[-baseDir <dir>] The base directory for H2 databases (all servers)
[-ifExists] Only existing databases may be opened (all servers)
[-trace] Print additional trace information (all servers)
[-key <from> <to>] Allows to map a database name to another (all servers)
The options -xAllowOthers are potentially risky.
For details, see Advanced Topics / Protection against Remote Access.
See also http://h2database.com/javadoc/org/h2/tools/Server.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[lixl@localhost h2db]$ java -cp h2*.jar org.h2.tools.Shell -?
Interactive command line tool to access a database using JDBC.
Usage: java org.h2.tools.Shell <options>
Options are case sensitive. Supported options are:
[-help] or [-?] Print the list of options
[-url "<url>"] The database URL (jdbc:h2:...)
[-user <user>] The user name
[-password <pwd>] The password
[-driver <class>] The JDBC driver class to use (not required in most cases)
[-sql "<statements>"] Execute the SQL statements and exit
[-properties "<dir>"] Load the server properties from this directory
If special characters don't work as expected, you may need to use
-Dfile.encoding=UTF-8 (Mac OS X) or CP850 (Windows).
See also http://h2database.com/javadoc/org/h2/tools/Shell.html

database_url

  • tcp
    java -cp h2-1.4.197.jar org.h2.tools.Server -tcpAllowOthers -webAllowOthers

  • jdbc-url-tcp

    1
    2
    3
    4
    driverClassName = org.h2.Driver
    url= jdbc:h2:tcp://localhost:9092/~/test
    username = test
    password = test
  • jdbc-url-local
    jdbc:h2:~/dbtest;MODE=Oracle;AUTO_SERVER=TRUE
    兼容性

  • jdbc-url-mem
    jdbc:h2:mem:test

  • console
    java -cp h2-1.4.197.jar org.h2.tools.Shell -url "jdbc:h2:tcp://192.168.18.147:9092/~/test" -user test -password test
    java -cp h2-1.4.197.jar org.h2.tools.Shell -url "jdbc:h2:/data/h2database/test" -user test -password test

  • maven exec-maven-plugin启动
    pom.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>h2-server</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
    <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.199</version>
    </dependency>
    </dependencies>

    <build>
    <plugins>
    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <executions>
    <execution>
    <goals>
    <goal>java</goal>
    </goals>
    </execution>
    </executions>
    <configuration>
    <mainClass>org.h2.tools.Server</mainClass>
    <arguments>
    <argument>-tcpAllowOthers</argument>
    <argument>-webAllowOthers</argument>
    <!-- db文件不存在时,可以直接创建一个新的,而不是报错 -->
    <argument>-ifNotExists</argument>
    </arguments>
    </configuration>
    </plugin>
    </plugins>
    </build>

    </project>

mvn ecec:java 等同于 java -cp h2-1.4.197.jar org.h2.tools.Server -tcpAllowOthers -webAllowOthers -ifNotExists

CSV操作

  • 通过数据库读取 CSV 文件

    1
    SELECT * FROM CSVREAD('test.csv');
  • 通过 CSV 文件导入数据

    1
    2
    3
    CREATE TABLE TEST AS SELECT * FROM CSVREAD('test.csv');
    CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))
    AS SELECT * FROM CSVREAD('test.csv');
  • 通过数据库写入CSV 文件

    1
    2
    3
    CREATE TABLE TEST(ID INT, NAME VARCHAR);
    INSERT INTO TEST VALUES(1, 'Hello'), (2, 'World');
    CALL CSVWRITE('test.csv', 'SELECT * FROM TEST');
  • java-Csv工具读写CSV文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    package com.sysware.p2m;

    import org.h2.tools.Csv;
    import org.h2.tools.SimpleResultSet;
    import org.junit.Test;

    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.sql.Types;

    public class H2CSVTest {

    String csv_file = "d://test2.csv";

    /**
    * 读写本地csv文件
    * @throws SQLException
    */
    @Test
    public void write() throws SQLException {
    SimpleResultSet rs = new SimpleResultSet();
    rs.addColumn("NAME", Types.VARCHAR, 255, 0);
    rs.addColumn("EMAIL", Types.VARCHAR, 255, 0);
    rs.addRow("Bob Meier", "bob.meier@abcde.abc");
    rs.addRow("John Jones", "john.jones@abcde.abc");
    new Csv().write(csv_file, rs, "UTF-8");
    }

    @Test
    public void read() throws SQLException {
    ResultSet rs = new Csv().read(csv_file, null, null);
    ResultSetMetaData meta = rs.getMetaData();
    while (rs.next()) {
    for (int i = 0; i < meta.getColumnCount(); i++) {
    System.out.println(
    meta.getColumnLabel(i + 1) + ": " +
    rs.getString(i + 1));
    }
    System.out.println();
    }
    rs.close();
    }

    }

连接池

1
org.h2.jdbcx.JdbcConnectionPool.create

参考

https://gitee.com/lixl/spring-schedule.git
h2使用文档
h2database