描述: 在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