首先下載H2 For Windows installer h2下載連接。html
安裝後在C:\Program Files\H2\src\test\org\h2\samples(默認安裝) 文件夾下有不少sample 。java
看看sample 代碼,不少就會了,sql
先來個helloworld ide
/*
* Copyright 2004-2010 H2 Group. Multiple-Licensed under the H2 License, Version
* 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group
*/
package org.h2.samples;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.h2.tools.DeleteDbFiles;
/**
* A very simple class that shows how to load the driver, create a database,
* create a table, and insert some data.
*/
public class HelloWorld {
/**
* Called when ran from command line.
*
* @param args ignored
*/
public static void main(String... args) throws Exception {
// delete the database named 'test' in the user home directory
DeleteDbFiles.execute("~", "test", true);
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:~/test");
Statement stat = conn.createStatement();
// this line would initialize the database
// from the SQL script file 'init.sql'
// stat.execute("runscript from 'init.sql'");
stat.execute("create table test(id int primary key, name varchar(255))");
stat.execute("insert into test values(1, 'Hello')");
ResultSet rs;
rs = stat.executeQuery("select * from test");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
conn.close();
}
}
this