连接:http://www.h2database.com/html/main.html


快速开始

  1. 引入 h2*.jar
  2. 加载驱动 org.h2.Driver
  3. 设置URL jdbc:h2:~/test

特征

  • 非常快,开源,JDBC API
  • 嵌入式和服务器模式;基于磁盘或内存中的数据库
  • 事务支持,多版本并发
  • 基于浏览器的控制台应用程序
  • 加密数据库
  • 全文搜索
  • 占用空间小的纯 Java:大约 2.5 MB 的 jar 文件大小
  • ODBC 驱动程序

H2数据库有三种运行方式实现

嵌入式(embedded):

可以同应用程序打包在一起发布,这样可以非常方便地存储少量结构化数据

服务模式:

1.TCP/IP server:支持客户端/服务器端的连接方式
2.web server:此种运行方式支持使用浏览器访问H2 Console
3.PG server:支持PostgreSQL客户端

内存方式:可以作为缓存,作为NoSQL的一个补充。

当某些场景下数据模型必须为关系型,可以拿它当Memcached使,作为后端MySQL/Oracle的一个缓冲层,缓存一些不经常变化但需要频繁访问的数据,比如字典表、权限表。

注意:H2查询 大小写不敏感,
数据库文件存储在哪里?
当使用像 jdbc:h2:~/test 这样的数据库 URL 时,数据库存储在用户目录中.对于 Windows,这通常是 C:Documents and Settings 或 C:Users.如果未设置基本目录(如jdbc:h2:./test),则数据库文件存储在应用程序启动的目录(当前工作目录)中.从开始菜单使用 H2 Console 应用程序时,这是 /bin.可以在数据库 URL 中设置基本目录.可以使用固定或相对路径.当使用 URL jdbc:h2:file:./data/sample 时,数据库存储在目录 data 中(相对于当前工作目录).如果该目录尚不存在

  1. private static String jdbcURL = "jdbc:h2:~/desgin";
  2. private static String jdbcUsername = "desgin";
  3. private static String jdbcPassword = "desgin";
  4. public static Connection getConnection() {
  5. Connection connection = null;
  6. try {
  7. connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
  8. } catch (SQLException e) {
  9. // TODO Auto-generated catch block
  10. e.printStackTrace();
  11. }
  12. return connection;
  13. }
  1. private static final String createTableSQL = "create table users\n" +
  2. "(\n" +
  3. "\tid bigint auto_increment,\n" +
  4. "\tname varchar(64),\n" +
  5. "\tage int,\n" +
  6. "\tconstraint USERS_PK\n" +
  7. "\t\tprimary key (id)\n" +
  8. ");\n";
  9. public void createTable() throws SQLException {
  10. // Step 1: Establishing a Connection
  11. try (Connection connection = H2JDBCUtils.getConnection();
  12. // Step 2:Create a statement using connection object
  13. Statement statement = connection.createStatement();) {
  14. // Step 3: Execute the query or update query
  15. statement.executeUpdate(createTableSQL);
  16. } catch (SQLException e) {
  17. // print SQL exception information
  18. H2JDBCUtils.printSQLException(e);
  19. }
  20. }
  1. private static final String QUERY = "select id,name,age from users where id =?";
  2. public static void main(String[] args) {
  3. // using try-with-resources to avoid closing resources (boiler plate code)
  4. // Step 1: Establishing a Connection
  5. try (Connection connection = H2JDBCUtils.getConnection();
  6. // Step 2:Create a statement using connection object
  7. PreparedStatement preparedStatement = connection.prepareStatement(QUERY);) {
  8. preparedStatement.setInt(1, 1);
  9. System.out.println(preparedStatement);
  10. // Step 3: Execute the query or update query
  11. ResultSet rs = preparedStatement.executeQuery();
  12. // Step 4: Process the ResultSet object.
  13. while (rs.next()) {
  14. int id = rs.getInt("id");
  15. String name = rs.getString("name");
  16. int email = rs.getInt("age");
  17. System.out.println(id + "," + name + "," + email );
  18. }
  19. } catch (SQLException e) {
  20. H2JDBCUtils.printSQLException(e);
  21. }
  22. // Step 4: try-with-resource statement will auto close the connection.
  23. }