Database
URLs
jdbc:h2:~/test 'test' in the user home
directory
jdbc:h2:/data/test 'test' in the directory
/data
jdbc:h2:test in the current(!) working
directory
jdbc:h2:mem:test multiple connections in
one process
jdbc:h2:mem: unnamed private; one
connection
jdbc:h2:tcp://localhost/~/test user home
dir
jdbc:h2:tcp://localhost//data/test absolute
dir
Server
start:java
-cp *.jar org.h2.tools.Server
jdbc:h2:..;MODE=MySQLcompatibility
(or HSQLDB,...)
jdbc:h2:..;TRACE_LEVEL_FILE=3log
to *.trace.db
测试代码:
package
cc;
import
java.sql.*;
public class
TestH2 {
public static
void main(String[] args) throws Exception {
Class.forName("org.h2.Driver");
//embedded
file
//Connection
conn = DriverManager.getConnection("jdbc:h2:~/testh2",
"sa","");
//embedded
mem
Connection conn
= DriverManager.getConnection("jdbc:h2:mem:",
"sa","");
System.out.println(conn);
createTable(conn);
insert(conn);
select(conn);
}
private static
void select(Connection conn) throws Exception {
String sql =
"Select ID, CONFIG_KEY, CONFIG_VALUE From
art_config";
ResultSet rs =
conn.createStatement().executeQuery(sql);
while(rs.next())
{
System.out.println(rs.getString(1) + ", " +
rs.getString(2) + ", " + rs.getString(3));
}
}
private static
void insert(Connection conn) throws Exception {
String sql
=
"insert into
art_config (ID, CONFIG_KEY, CONFIG_VALUE, REMARK) "
+
" values (2,
'quartzServerIps', '172.21.127.29', null)";
int i =
conn.createStatement().executeUpdate(sql);
System.out.println(i);
}
private static
void createTable(Connection conn) throws Exception
{
String sql
=
"create table
ART_CONFIG\n" +
"(\n"
+
"
id NUMBER(19)
not null,\n" +
"
config_key VARCHAR2(50) not
null,\n" +
"
config_value VARCHAR2(4000),\n"
+
"
remark VARCHAR2(500)\n"
+
")";
conn.createStatement().execute(sql);
}
}