下面是这3个轻量级的数据库的使用简单例子,其中有些数据库支持内存,文件,网络等方式连接,本篇侧重基于内存,和文件连接的使用,代码如下:
(1),derby的使用:
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(); } }
(2)hsqldb的使用:
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(); } }
(3)h2的使用:
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(); } }
其中h2和hsqldb是非常类似的,但是又有很大的不同,通过下面的一张对比表,我们可以看出来他们之间有什么不同之处:
其实,散仙做这些对比,也是为了了解一下他们不同之处,使用内存库,用来统计一些不需要持久化操作的数据,而且数量不大的数据,是非常方便的。