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内嵌如果不能自动建库会很不方便)

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 @ 2022-02-21 13:32  锐洋智能  阅读(703)  评论(0编辑  收藏  举报