上篇文章散仙,写了关于SQLite的使用,本篇我们看下几款JAVA开源的数据库,主要有3个,derby,H2,HSQLdb,都是非常不错的数据库,既可以当
内存库,又可以嵌入移动
设备充当数据存储的角色,而且完全支持
关系型数据库的SQL语法,所以适当了解一些,对以后的数据存储的,或计算会有更好的辅助性。
下面是这3个轻量级的数据库的使用简单
例子,其中有些数据库支持内存,文件,网络等方式连接,本篇侧重基于内存,和文件连接的使用,代码如下:
(1),derby的使用:
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();
}
}
(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是非常类似的,但是又有很大的不同,通过下面的一张对比表,我们可以看出来
他们之间有什么不同之处:
其实,散仙做这些对比,也是为了了解一下他们不同之处,使用内存库,用来统计一些不需要持久化操作的数据,而且数量不大的数据,是非常方便的。
- 大小: 463.1 KB
- 驱动jar.zip (6.2 MB)
- 下载次数: 0