一)H2文件结构:
————————————————————————————————
-h2
-bin
h2-1.1.116.jar //H2主文件(驱动也在里面)
h2.bat //H2控制台启动程序带黑屏窗口(for Windows)
h2.sh //H2控制台启动程序(for Linux)
h2w.bat //H2控制台启动程序不带黑屏窗口(for Windows)
+docs //文档文件夹(内有手册)
+service
+src //开源的吗,带源代码
build.bat
build.sh
build.xml
————————————————————————————————
(二)Eclipse/MyEclipse中引入H2驱动
在指定项目中右键 -> Peoperties -> Java Build Path -> Libraries -> Add External JARs -> 引入h2-1.1.116.jar
(三)使用内存模式(关闭后内容全部消失,速度非常快):
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MenTest{
public void runInsertDelete(){
try{
String sourceURL="jdbc:h2:tcp://localhost/mem:testmemdb";//H2DB mem mode
String user="sa";
String key="";
try{
Class.forName("org.h2.Driver");//HSQLDB Driver
}catch(Exception e){
e.printStackTrace();
}
Connection conn=DriverManager.getConnection(sourceURL,user,key);//把驱动放入连接
Statement stmt=conn.createStatement();
//创建一个 Statement 对象来将 SQL 语句发送到数据库。
//stmt.executeUpdate("DELETE FROM mytable WHERE name=\'NO.2\'");
//执行方法找到一个与 methodName 属性同名的方法,并在目标上调用该方法。
//stmt.execute("CREATE TABLE idtable(id INT,name VARCHAR(100));");
stmt.execute("INSERT INTO idtable VALUES(1,\'MuSoft\')");
stmt.execute("INSERT INTO idtable VALUES(2,\'StevenStander\')");
stmt.close();
conn.close();
}catch(SQLException sqle){
System.out.println("SQL ERROR!");
}
}
public void query(String SQL){
try{
String sourceURL="jdbc:h2:tcp://localhost/mem:testmemdb";
String user="sa";
String key="";
try{
Class.forName("org.h2.Driver");
}catch(Exception e){
e.printStackTrace();
}
Connection conn=DriverManager.getConnection(sourceURL,user,key);//把驱动放入连接
Statement stmt=conn.createStatement();//创建一个 Statement 对象来将 SQL 语句发送到数据库。
ResultSet rset=stmt.executeQuery(SQL);//执行方法找到一个与 methodName 属性同名的方法,并在目标上调用该方法。
while(rset.next())
{
System.out.println(rset.getInt("id")+" "+rset.getString("name"));
}
rset.close();
stmt.close();
conn.close();
}catch(SQLException sqle){
System.err.println(sqle);
}
}
public static void main(String args[]){
MenTest mt=new MenTest();
mt.runInsertDelete();
mt.query("SELECT * FROM idtable");
}
}
(四)使用Embedded模式(Create,Insert,Delete...):
import java.sql.*;
public class UpdateTest {
public void runInsertDelete() {
try {
String sourceURL = "jdbc:h2:h2/bin/mydb";// H2 database
String user = "sa";
String key = "";
try {
Class.forName("org.h2.Driver");// H2 Driver
} catch (Exception e) {
e.printStackTrace();
}
Connection conn = DriverManager.getConnection(sourceURL, user, key);
Statement stmt = conn.createStatement();
stmt.execute("CREATE TABLE mytable(name VARCHAR(100),sex VARCHAR(10))");
stmt.executeUpdate("INSERT INTO mytable VALUES('Steven Stander','male')");
stmt.executeUpdate("INSERT INTO mytable VALUES('Elizabeth Eames','female')");
stmt.executeUpdate("DELETE FROM mytable WHERE sex=\'male\'");
stmt.close();
conn.close();
} catch (SQLException sqle) {
System.err.println(sqle);
}
}
public static void main(String args[]) {
new UpdateTest().runInsertDelete();
}
}
(五)使用Embedded模式(Select):
import java.sql.*;
public class SelectTest {
public void query(String SQL) {
try {
String sourceURL = "jdbc:h2:h2/bin/mydb";
String user = "sa";
String key = "";
try {
Class.forName("org.h2.Driver");
} catch (Exception e) {
e.printStackTrace();
}
Connection conn = DriverManager.getConnection(sourceURL, user, key);
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery(SQL);
while (rset.next()) {
System.out.println(rset.getString("name")+ " "+rset.getString("sex"));
}
rset.close();
stmt.close();
conn.close();
} catch (SQLException sqle) {
System.err.println(sqle);
}
}
public static void main(String args[]) {
Test tt = new Test();
tt.query("select * from mytable");
}
}