小编Gar*_*ett的帖子

Java继承中的"this"关键字如何工作?

在下面的代码片段中,结果确实令人困惑.

public class TestInheritance {
    public static void main(String[] args) {
        new Son();
        /*
        Father father = new Son();
        System.out.println(father); //[1]I know the result is "I'm Son" here
        */
    }
}

class Father {
    public String x = "Father";

    @Override
    public String toString() {
       return "I'm Father";
    }

    public Father() {
        System.out.println(this);//[2]It is called in Father constructor
        System.out.println(this.x);
    }
}

class Son extends Father {
    public String x = "Son";

    @Override
    public String toString() {
        return "I'm Son";
    }
} …
Run Code Online (Sandbox Code Playgroud)

java polymorphism inheritance this

27
推荐指数
4
解决办法
4735
查看次数

如何在控制台中连接到WebServer模式H2数据库?

在H2文档中,它表示只有Web服务器支持浏览器连接.这是否意味着我们只能在WebServer模式下通过控制台访问H2数据库,而不是TcpServer?但是,当我在下面进行测试时,结果完全不如预期.

public class TestMem {
    public static void main(String... args) throws Exception {

        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:mem:test");
        conn.createStatement().execute("create table test(id int)");

        Server server = Server.createTcpServer().start();//1.TcpServer
//      Server server = Server.createWebServer().start();//2.WebServer

        System.out.println("Server started and connection is open.");
        System.out.println("URL: jdbc:h2:" + server.getURL() + "/mem:test");

        Thread.sleep(5*60*1000);

        System.out.println("Stopping server and closing the connection");
        server.stop();
        conn.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我启动一个TcpServer,我可以通过这个url访问数据库:jdbc:h2:tcp:// localhost:9092/mem:test in console.

//Use TcpServer
  Server server = Server.createTcpServer().start();
Run Code Online (Sandbox Code Playgroud)

但是当我启动WebServer时,我尝试连接使用jdbc:h2:http:// localhost:8082/mem:test,下面将抛出异常:IO例外:"java.io.IOException:文件名,目录名或者卷标语法不正确"; "http:// localhost:8082/mem:test.h2.db"[90031-172] 90031/90031(帮助).

//Use WebServer
  Server server = …
Run Code Online (Sandbox Code Playgroud)

h2

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

h2 ×1

inheritance ×1

java ×1

polymorphism ×1

this ×1