eclipse 如何从 Java 和 H2 DB 连接到 H2 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34248614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:59:04  来源:igfitidea点击:

How to connect to H2 database from Java & H2 DB

javadatabaseeclipsejdbch2

提问by seulberg1

I am fairly new to Eclipse & Java and currently work on a project where I need to implement my first database.

我对 Eclipse 和 Java 还很陌生,目前正在从事一个需要实现我的第一个数据库的项目。

Therefore, I tried to connect Eclipse and the H2 Database. While the H2 database part works just fine by itself, I can't figure out how to connect it to Eclipse.

因此,我尝试连接 Eclipse 和 H2 数据库。虽然 H2 数据库部分本身工作得很好,但我不知道如何将它连接到 Eclipse。

I created the following class and tried to do everything as stated on the website:

我创建了以下课程并尝试按照网站上的说明执行所有操作:

package srpTracking;

import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.h2.tools.DeleteDbFiles;

public class DatabaseConnector {

    public static void main(String... args) throws Exception {
        DeleteDbFiles.execute("~", "test", true);

        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:~/test");
        Statement stat = conn.createStatement();


        stat.execute("create table test(id int primary key, name varchar(255))");
        stat.execute("insert into test values(1, 'Hello')");
        ResultSet rs;
        rs = stat.executeQuery("select * from test");
        while (rs.next()) {
            System.out.println(rs.getString("name"));
        }
    }
        stat.close();
        conn.close();
 } 

I get identifier errors for the last two lines of code, but it also doesn't connect anything.

我收到最后两行代码的标识符错误,但它也没有连接任何东西。

I copied the H2.jar file into the project folder in a subfolder called lib.

我将 H2.jar 文件复制到名为 lib 的子文件夹中的项目文件夹中。

Unfortunately I can't for some reason install the DTP plugin, because I am apparently missing a 'org.eclipse.core.runtime' file.

不幸的是,由于某种原因我无法安装 DTP 插件,因为我显然缺少一个“org.eclipse.core.runtime”文件。

What do I need to change about my code connect Java and H2? Also, do I need to copy and H2 files into specific folders?

我需要对连接 Java 和 H2 的代码进行哪些更改?另外,我是否需要将 H2 文件复制到特定文件夹中?

采纳答案by Jan

Consider these changes to your main. By using try-with-resource you delegate the close to the JVM - with the added benefit that even in cases of exceptions, you wouldn't need to close them as well.

考虑对您的 main.js 进行这些更改。通过使用 try-with-resource,您可以将关闭委托给 JVM - 具有额外的好处,即使在出现异常的情况下,您也不需要关闭它们。

Why it didn't work before? Your variables ran out of scope- you tried to close aftermain() had ended.

为什么以前不起作用?您的变量超出范围- 您试图main() 结束关闭。

public static void main(String... args) throws Exception {
    DeleteDbFiles.execute("~", "test", true);

    Class.forName("org.h2.Driver");
    try (Connection conn = DriverManager.getConnection("jdbc:h2:~/test"); 
            Statement stat = conn.createStatement()) {
        stat.execute("create table test(id int primary key, name varchar(255))");
        stat.execute("insert into test values(1, 'Hello')");
        try (ResultSet rs = stat.executeQuery("select * from test")) {
            while (rs.next()) {
                System.out.println(rs.getString("name"));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}