1、找到h2数据库的jar包
D:\repositories\com\h2database\h2\1.4.187\h2-1.4.187.jar
2、双击jar包,配置连接信息
Driver Class:org.h2.Driver
JDBC URL:jdbc:h2:file:~/.h2/sample;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=-1(与application.properties相同,但掉"log4jdbc:")
3、点击连接,在T_USER表中插入一条数据
insert into t_user (ID, EMAIL, NAME, PASSWORD, ROLE, SALT, SEX)
values ('fbe00c9e-97b0-49f9-9f3d-3431050dec74', 'xy.hero@qq.com', 'admin', '4c1e9c2a03a6f49db3bde4699fcb98fe5f1203b8', 'admin', '9c14b4497360e049', '1');
新建h2数据库
指定JDBC URL:jdbc:h2:E://research//workspace//H2Test//db//test,点击connect,会看到e盘多出一个文件夹research
执行test语句,创建test表
测试类
package cn.itcast.test; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* H2测试类
* @author wangxiangyu
*/
public class TestH2 { public static void main(String[] args) throws ClassNotFoundException, SQLException{
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:E://research//workspace//H2Test//db//test", "sa", "");
Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from test");
while(rs.next()){
System.out.println(rs.getInt("ID")+","+rs.getString("NAME"));
}
conn.close();
}
}