https://www.cnblogs.com/xdp-gacl/p/4171024.html
中文教程:https://blog.youkuaiyun.com/daqiang012/article/details/81069894
创建数据库
运行./bin/h2.bat,启动h2
打开localhost:8082;默认数据库为test,无密码。
修改JDBC URL(规则:jdbc:h2:path/dbName),用户名、密码;创建数据库。./为相对路径。
配置文件位置:C:\Users\username.h2.server.properties
连接数据库
- 内嵌方式
Generic H2(Embedded)
url:jdbc:h2:absolutPath\dbName
只允许一个客户端连接。 - 服务器方式
Generic H2(Server)
url:jdbc:h2:tcp://IP:PORT/absolutPath/dbName
允许多个客户端连接。 - 内存模式
url:jdbc:h2:tcp://localhost/mem:gacl
数据库关闭后数据消失。
Web中使用H2
启停H2
Server类型:
- WebServer;h2 console,支持浏览器连接
- TcpServer;c/s connection
- PgServer;for postgreSql clients
Server server = Server.createTcpServer().start();
server.stop();
参数:
- -tcpAllowOthers;允许其他电脑连接
- -tcpDaemon;使用守护线程运行
- -tcpPort <port>;端口 (default: 9092)
- -tcpSSL;使用https
- -tcpPassword <pwd>;关闭tcpServer的密码
- -tcpShutdown <url>;关闭tcpServer的url
- -tcpShutdownForce;强制关闭
- -baseDir <dir>;数据库根文件夹
- -ifExists;db存在时才允许连接
- -ifNotExists;db不存在时新建
- -trace;打印跟踪信息
2,SpringBoot整合
1,springboot启停h2
application生命周期、或配置bean并指定initMethod/destroyMethod
通过@DependsOn、@AutoConfigureAfter/@AutoConfigureBefore控制server在datasource前加载。
2,配置浏览器访问
spring:
h2:
console: #h2浏览器监控界面
enabled: true
path: /h2console
settings:
web-allow-others: true #远程访问
访问url:/h2console
3,修改用户
–创建用户 CREATE USER IF NOT EXISTS fileconvert { PASSWORD ‘fileconvert’ };
–修改fileconvert密码 ALTER USER fileconvert SET { PASSWORD ‘fileconvert’ };
–用户授权 ALTER USER fileconvert ADMIN { TRUE };
–删除用户 DROP USER IF EXISTS fileconvert;
其他
1,读写csv文件,bin/路径:
insert into test select * from csvread(‘test.csv’);
call csvwrite(‘test.csv’,‘select * from test’);