H2 database 安装与使用

来源:互联网 发布:动态加载js 完成 编辑:程序博客网 时间:2024/04/30 00:12

亲爱的人,今天来学习H2 这个pured Java 的database,在Ubuntu 12.04

选择学习这个是因为我想写一个数据库,使用Java语言,而这个是最好的参考。

首先我们找到官方网站: H2 Database Engine

但是在Ubuntu下好像下载不了H2, 因此首先下载了H2-1.4.181-sources源码和API文档,这些都在官网上有

查看了官方的cheat sheet(备忘单):

首先是一个相当简单的使用 说明 :

Using H2

  • H2 is open source, free to use and distribute.
  • Download:jar, installer (Windows), zip.
  • To start the H2 Console tool, double click the jar file, or run java -jar h2*.jar, h2.bat, or h2.sh.
  • A new database is automatically createdby default.
  • Closing the last connection closes the database.
好我们来试试:

出现了 没有主清单属性。看来这个真的只是源码。

然后在客户端输入:

弹出:

算是启动了,然后弹出个界面:

OK,接着我们来看一看H2 database的一些指令吧:

首先打开 H2 的 console:

       1. 找到bin 目录下 的 h2-版本好-sources.jar

       2. java -cp h2*.jar org.h2.tools.Shell

打开界面如下:

      

如果在这里输入了错误的 命令:会抛出Java错误,再一次证明了这是个pure java Database.

由于时间关系,指令查看完毕。尝试一下 jdbc 与 H2 的连接,

import java.sql.*;import org.h2.jdbcx.JdbcConnectionPool;public class H2connection {public static void main(String[] args) throws Exception {JdbcConnectionPool cp = JdbcConnectionPool.create("jdbc:h2:tcp://localhost/~/test", "Poros", "Poros");String sqlStatement = "select * from poros;";Connection conn = cp.getConnection();ResultSet Poros = conn.createStatement().executeQuery(sqlStatement);while(Poros.next()){int i = Poros.getInt("id");String name = Poros.getString("name");System.out.println("It is ID : "+i);System.out.println("It is Name : "+ name);}conn.close();cp.dispose();}}
上述程序能成功输出结果。

H2 能与Hibernate等ORM框架整合,详细可以参考官方文档。

H2的安装暂且到这里结束。



0 0