2、解压文件,这里以%H2_HOME%表示为解压的文件目录。运行%H2_HOME%\bin\h2.bat 将会自动打开下面网址。(请确认是否安装了jdk,并设置了JAVA_HOME环境变量)
http://192.168.140.1:8082/login.jsp?jsessionid=244e36a683f97f0d4f3b000f33530ed1
3、点击 connect ,登录。
4、执行上图中红色部分sql语句,成功创建test表。
因为没有指定数据库文件位置,会自动输出到输出到C:\Users\Administrator下。
H2文件结构
%H2_HOME%
-h2
-bin
h2-1.3.154.jar //jar包
h2.sh //Linux控制台启动脚本
h2w.bat//Windows控制台启动脚本(不带黑屏窗口)
+docs 帮助文档
+service //通过wrapper包装成服务。
+src //源代码
build.bat windows构建脚本
build.sh linux构建脚本
简单来说就是用jdbc:h2:mem:h2db来建立内存模式,并建表,然后jdbc:h2:tcp://192.168.20.141:8082/mem:h2db来访问上面的内存数据库
package test
import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Statement
import org.h2.tools.Server
public class H2Demo {
private Server server
private String port = "8082"
private static String sourceURL1 = "jdbc:h2:mem:h2db"
private static String sourceURL2 = "jdbc:h2:tcp://192.168.20.141:8082/mem:h2db"
private String user = "shorturl"
private String password = "123456"
public void startServer() {
try {
System.out.println("正在启动h2...")
server = Server.createTcpServer(
new String[] { "-tcpPort", port }).start()
} catch (SQLException e) {
System.out.println("启动h2出错:" + e.toString())
// TODO Auto-generated catch block
e.printStackTrace()
throw new RuntimeException(e)
}
}
public void stopServer() {
if (server != null) {
System.out.println("正在关闭h2...")
server.stop()
System.out.println("关闭成功.")
}
}
public void useH2() {
try {
Class.forName("org.h2.Driver")
Connection conn = DriverManager.getConnection(sourceURL1,user, password)
Statement stat = conn.createStatement()
// insert data
stat.execute("CREATE MEMORY Table TEST(NAME VARCHAR)")
stat.execute("INSERT INTO TEST VALUES('Hello World')")
//stat.execute("delete mappedURL")
// use data
ResultSet result = stat.executeQuery("select name from test ")
int i = 1
while (result.next()) {
System.out.println(i++ + ":" + result.getString("name"))
}
result.close()
stat.close()
conn.close()
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
public void useH2i() {
try {
Class.forName("org.h2.Driver")
//Connection conn = DriverManager.getConnection("jdbc:h2:" + dbDir+"AUTO_SERVER=TRUEMVCC=TRUE",user, password)
Connection conn = DriverManager.getConnection(sourceURL2,user, password)
Statement stat = conn.createStatement()
// use data
ResultSet result = stat.executeQuery("select name from test")
写HelloWorld的方便工具。依靠初始化脚本,数据随时重置。交流起来,也方便。
maven
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.194</version>
</dependency>
web.xml中配置
<context-param>
<param-name>db.url</param-name>
<param-value>jdbc:h2:mem:test</param-value>
</context-param>
<context-param>
<param-name>db.user</param-name>
<param-value>sa</param-value>
</context-param>
<context-param>
<param-name>db.password</param-name>
<param-value>sa</param-value>
</context-param>
<context-param>
<param-name>db.tcpServer</param-name>
<param-value>-tcpAllowOthers</param-value>
</context-param>
<listener>
<listener-class>org.h2.server.web.DbStarter</listener-class>
</listener>
<servlet>
<servlet-name>H2Console</servlet-name>
<servlet-class>org.h2.server.web.WebServlet</servlet-class>
<init-param><param-name>webAllowOthers</param-name><param-value></param-value></init-param>
<init-param><param-name>trace</param-name><param-value></param-value></init-param>
</servlet>
<servlet-mapping>
<servlet-name>H2Console</servlet-name>
<url-pattern>/h2/*</url-pattern>
</servlet-mapping>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)