先对H2 Database做一些简单的介绍
-
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