描述: 在Windows下学习SpringBoot项目中使用H2数据库报错(Linux不会出现此问题)
[ restartedMain] o.h.e.j.e.i.JdbcEnvironmentInitiator : HHH000342: Could not obtain connection to query metadata : A file path that is implicitly relative to the current working directory is not allowed in the database URL "jdbc:h2:/data/h2/test". Use an absolute path, ~/name, ./name, or the baseDir setting instead. [90011-197]
解决方案:
// 方案一:
public DataSource dataSource(){
JdbcDataSource dataSource = new JdbcDataSource();
System.setProperty("h2.baseDir", "D:/data/h2");
// 如果非要使用自定义的地址需要设置系统属性,h2的根目录
dataSource.setUrl("jdbc:h2:/test");//当前数据位置在D:/data/h2目录下
dataSource.setUser("sa");
dataSource.setPassword("");
return dataSource;
}
public DataSource dataSource(){
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setUrl("jdbc:h2:~/test");//当前数据库位置在家目录下 // 方案二
//dataSource.setUrl("jdbc:h2:./test");//当前数据库位置在当前项目的根目录下 // 方案三
dataSource.setUser("sa");
dataSource.setPassword("");
return dataSource;
}
分析: 在Windows下不能创建/data/h2目录造成的
参考链接:
http://www.h2database.com/html/features.html#database_url
https://www.h2database.com/html/cheatSheet.html