H2 DataBase 数据库操作

先对H2 Database做一些简单的介绍

h2是Thomas Mueller提供的一个开源的、纯java实现的关系数据库。
h2数据库特点
(1)性能、小巧
(2)同时支持网络版和嵌入式版本,另外还提供了内存版
(3)有比较好的兼容性,支持相当标准的sql标准
(4)提供了非常友好的基于web的数据库管理界面
Welcome to H2, the Java SQL database. The main features of H2 are:
  • Very fast, open source, JDBC API
  • Embedded and server modes; in-memory databases
  • Browser based Console application
  • Small footprint: around 1.5 MB jar file size

操作示例:

public static void main(String[] args) {
  Server server = null;
  try {
   server = Server.createTcpServer(args).start();
   
   Class.forName("org.h2.Driver");
   // C:/data/sample表示数据库文件放在C:/data/目录下,数据库名叫sample(也可以以Ip地址形式,官方文档中有)
   Connection conn = DriverManager.getConnection("jdbc:h2:file:C:/data/sample", "sa", "");
   PreparedStatement ps = conn
           .prepareStatement("create table excel(`time` double NOT NULL,`location` char(100) default NULL,`data` bigint(20) NOT NULL)");
   PreparedStatement ps2 = conn.prepareStatement("create index idx_time on excel(time)");
   PreparedStatement ps3 = conn.prepareStatement("create index idx_location on excel(location)");
   PreparedStatement ps4 = conn.prepareStatement("create index idx_data on excel(data)");
   
   ps.execute();
   ps2.execute();
   ps3.execute();
   ps4.execute();
   PreparedStatement ps5 = conn.prepareStatement("insert into excel values(0,'c1',1)");
   ps5.execute();
   PreparedStatement ps6 = conn.prepareStatement("select * from excel");
   ResultSet rs = ps6.executeQuery();
   while (rs.next()) {
    double time = rs.getDouble(1);
    String location = rs.getString(2);
    int data = rs.getInt(3);
    System.out.println("time:" + time + "-location:" + location + "-data:" + data);
   }
   try {
    ps.close();
    ps2.close();
    ps3.close();
    ps4.close();
    ps5.close();
    ps6.close();
    conn.close();
   }
   catch (SQLException e) {
    e.printStackTrace();
   }
  }
  catch (SQLException e) {
   e.printStackTrace();
  }
  catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
  finally {
   server.stop();
  }
 }


来源:

http://yangchao20020.blog.163.com/blog/static/4838224720088321029343/

H2官方网站:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值