内存数据库

derby

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


/**
* Created by lifei on 16/7/14.
*/
public class Derby {
public static void main(String[] args) {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:derby:memory:myDB;create=true");

Statement st = conn.createStatement();
st.execute("create table test2 (id int, name varchar(20))");
st.execute("insert into test2 values(1,'sinboy')");
st.execute("insert into test2 values(2,'Tom')");

ResultSet rs = st.executeQuery("select * from test2");
// ResultSet rs = st.executeQuery("SELECT * from test2 where name REGEXP '^T'");//不支持
while (rs.next()) {
System.out.println("id:" + rs.getInt(1) + " name:" + rs.getString(2));
}
rs.close();
st.close();
conn.commit();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

sqllite

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.qnr.parsesql;

import java.sql.*;

/**
* Created by lifei on 16/7/14.
*/
public class Sqllite {
public static void main(String[] args) throws Exception{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite::memory:");
Statement stat = conn.createStatement();
stat.executeUpdate("drop table if exists people;");
stat.executeUpdate("create table people (name, occupation);");
PreparedStatement prep = conn.prepareStatement(
"insert into people values (?, ?);");

prep.setString(1, "Gandhi");
prep.setString(2, "politics");
prep.addBatch();
prep.setString(1, "Turing");
prep.setString(2, "computers");
prep.addBatch();
prep.setString(1, "Wittgenstein");
prep.setString(2, "smartypants");
prep.addBatch();

conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);

// ResultSet rs = stat.executeQuery("select * from people;");
ResultSet rs = stat.executeQuery("select * from people where name REGEXP '^G'"); //不支持
while (rs.next()) {
System.out.println("name = " + rs.getString("name"));
System.out.println("job = " + rs.getString("occupation"));
}
rs.close();
conn.close();
}
}

h2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.UUID;

/**
* <p>ClassName: H2ConnTest1<p>
* <p>Description: Java通过JDBC方式连接H2数据库<p>
* [[@author](http://my.oschina.net/arthor)](http://my.oschina.net/arthor) xudp
* [[@version](http://my.oschina.net/u/931210)](http://my.oschina.net/u/931210) 1.0 V
* @createTime 2014-12-18 上午11:22:12
*/
public class H2ConnTest1 {
//数据库连接URL,当前连接的是E:/H2目录下的gacl数据库
private static final String JDBC_URL = "jdbc:h2:mem:mydb";
//连接数据库时使用的用户名
private static final String USER = "";
//连接数据库时使用的密码
private static final String PASSWORD = "";
//连接H2数据库时使用的驱动类,org.h2.Driver这个类是由H2数据库自己提供的,在H2数据库的jar包中可以找到
private static final String DRIVER_CLASS="org.h2.Driver";

public static void main(String[] args) throws Exception {
// 加载H2数据库驱动
Class.forName(DRIVER_CLASS);
// 根据连接URL,用户名,密码获取数据库连接
Connection conn = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
//如果存在USER_INFO表就先删除USER_INFO表
stmt.execute("DROP TABLE IF EXISTS USER_INFO");
//创建USER_INFO表
stmt.execute("CREATE TABLE USER_INFO(id VARCHAR(36) PRIMARY KEY,name VARCHAR(100),sex VARCHAR(4))");
//新增
stmt.executeUpdate("INSERT INTO USER_INFO VALUES('" + UUID.randomUUID()+ "','大日如来','男')");
stmt.executeUpdate("INSERT INTO USER_INFO VALUES('" + UUID.randomUUID()+ "','青龙','男')");
stmt.executeUpdate("INSERT INTO USER_INFO VALUES('" + UUID.randomUUID()+ "','白虎','男')");
stmt.executeUpdate("INSERT INTO USER_INFO VALUES('" + UUID.randomUUID()+ "','朱雀','女')");
stmt.executeUpdate("INSERT INTO USER_INFO VALUES('" + UUID.randomUUID()+ "','玄武','男')");
stmt.executeUpdate("INSERT INTO USER_INFO VALUES('" + UUID.randomUUID()+ "','苍狼','男')");
//删除
stmt.executeUpdate("DELETE FROM USER_INFO WHERE name='大日如来'");
//修改
stmt.executeUpdate("UPDATE USER_INFO SET name='孤傲苍狼' WHERE name='苍狼'");
//查询
// ResultSet rs = stmt.executeQuery("SELECT * FROM USER_INFO");
ResultSet rs = stmt.executeQuery("SELECT * FROM USER_INFO where name REGEXP '^大|白'");
//遍历结果集
while (rs.next()) {
System.out.println(rs.getString("id") + "," + rs.getString("name")+ "," + rs.getString("sex"));
}
//释放资源
stmt.close();
//关闭连接
conn.close();
}
}

maven

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.8.11.2</version>
</dependency>

<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.8.3.0</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.192</version>
</dependency>