h2:无法自动建库解决(H2 Database Engine)

1.4.198 (2019-02-22) 版本开始,H2不再自动创建数据库,很难在网上找到有解决方案,故down了源码

 

 Connection to jdbc:h2:tcp://0.0.0.0:6666/./aika failed. [90149][90149] Database "/home/Hex/IdeaProjects/h2database/h2/aika" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-200]

翻阅源码在

tutorial.html中有这么一段话(官方是不推荐这么做的,但是我们有时候用h2内嵌如果不能自动建库会很不方便)

1
2
3
4
5
If you really need to allow remote database creation, you can pass <code>-ifNotExists</code> parameter to
TCP, PG, or Web servers (but not to the Console tool).
Its combination with <code>-tcpAllowOthers</code>, <code>-pgAllowOthers</code>, or <code>-webAllowOthers</code>
effectively creates a remote security hole in your system, if you use it, always guard your ports with a firewall
or some other solution and use such combination of settings only in trusted networks.

 在启动服务的时候添加  -ifNotExists 就可以了。

复制代码
package org.h2.start;
 
import org.h2.tools.Server;
 
 
public class StartServer {
 
    public static void main(String[] args) throws Exception{
        Server server = Server.createTcpServer("-tcp", "-tcpAllowOthers","-ifNotExists", "-tcpPort",
                "6666");
 
        server.start();
        while (true){
            Thread.sleep(10000);
        }
    }
}
复制代码

 

posted @   锐洋智能  阅读(764)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
· Windows 提权-UAC 绕过
点击右上角即可分享
微信分享提示