浏览 2019 次
锁定老帖子 主题:JAVA内存数据库使用demo
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2014-10-13
下面是这3个轻量级的数据库的使用简单例子,其中有些数据库支持内存,文件,网络等方式连接,本篇侧重基于内存,和文件连接的使用,代码如下: (1),derby的使用: <pre name="code" class="java">package com.derby.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class DerbyTest { public static void main(String[] args) throws Exception{ Connection connection = null; Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); connection = DriverManager.getConnection("jdbc:derby:dy;create=true"); Statement statement = connection.createStatement(); statement.setQueryTimeout(30); // set timeout to 30 sec. // statement.executeUpdate("drop table if exists person "); statement.executeUpdate("drop table person "); statement.executeUpdate("create table person (id integer, name varchar(50))"); statement.executeUpdate("insert into person values(1, '我是第一个学生')"); statement.executeUpdate("insert into person values(2, '中国人')"); statement.executeUpdate("insert into person values(45, '外国人')"); statement.executeUpdate("insert into person values(4, '中国人')"); ResultSet rs = statement.executeQuery("select * from person order by name "); while(rs.next()){ System.out.println("id: "+rs.getInt("id")+" name: " + rs.getString("name")); } rs.close(); statement.close(); connection.close(); } } </pre> (2)hsqldb的使用: <pre name="code" class="java">package com.hsqldb.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class HSQLdbTest { public static void main(String[] args) throws Exception{ Connection connection = null; Class.forName("org.hsqldb.jdbcDriver"); //内存计算 connection = DriverManager.getConnection("jdbc:hsqldb:mem"); //支持持久存储 //connection = DriverManager.getConnection("jdbc:hsqldb:file:hsql.db"); Statement statement = connection.createStatement(); statement.setQueryTimeout(30); // set timeout to 30 sec. statement.executeUpdate("drop table if exists person "); //statement.executeUpdate("drop table person "); statement.executeUpdate("create table person (id integer, name varchar(50))"); statement.executeUpdate("insert into person values(1, '我是第一个学生')"); statement.executeUpdate("insert into person values(2, '中国人')"); statement.executeUpdate("insert into person values(45, '外国人')"); statement.executeUpdate("insert into person values(4, '中国人')"); ResultSet rs = statement.executeQuery("select * from person order by id "); while(rs.next()){ System.out.println("id: "+rs.getInt("id")+" name: " + rs.getString("name")); } rs.close(); statement.close(); connection.close(); } } </pre> (3)h2的使用: <pre name="code" class="java">package com.h2.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class H2Test { public static void main(String[] args) throws Exception{ Connection connection = null; Class.forName("org.h2.Driver"); //内存计算 connection = DriverManager.getConnection("jdbc:h2:mem:"); //支持持久存储 //connection = DriverManager.getConnection("jdbc:h2:file:./bb.bb"); //connection = DriverManager.getConnection("jdbc:h2:./bb.bb"); Statement statement = connection.createStatement(); statement.setQueryTimeout(30); // set timeout to 30 sec. statement.executeUpdate("drop table if exists person "); //statement.executeUpdate("drop table person "); statement.executeUpdate("create table person (id integer, name varchar(50))"); statement.executeUpdate("insert into person values(1, '我是第一个学生')"); statement.executeUpdate("insert into person values(2, '中国人')"); statement.executeUpdate("insert into person values(45, '外国人')"); statement.executeUpdate("insert into person values(4, '中国人')"); ResultSet rs = statement.executeQuery("select * from person order by id "); while(rs.next()){ System.out.println("id: "+rs.getInt("id")+" name: " + rs.getString("name")); } rs.close(); statement.close(); connection.close(); } } </pre> 其中h2和hsqldb是非常类似的,但是又有很大的不同,通过下面的一张对比表,我们可以看出来他们之间有什么不同之处: 其实,散仙做这些对比,也是为了了解一下他们不同之处,使用内存库,用来统计一些不需要持久化操作的数据,而且数量不大的数据,是非常方便的。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |