Java 没有配置数据源来运行此 SQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42933570/
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-08-12 00:31:21  来源:igfitidea点击:

No data sources are configured to run this SQL

javasqliteintellij-idea

提问by Ingialldus

I have a little problem with the creation of a table (for a database) in Java.

我在 Java 中创建表(用于数据库)时遇到了一些问题。

Currently, I'm using IntelliJ IDEA and when I write the code for creating a table the text is highlighted in yellow and when I look to the problem I see the following message:

目前,我正在使用 IntelliJ IDEA,当我编写用于创建表格的代码时,文本以黄色突出显示,当我查看问题时,我看到以下消息:

"No data sources are configured to run this SQL and provide advanced code assistance. Disable this inspection via problem menu (??)."

“没有配置数据源来运行此 SQL 并提供高级代码帮助。通过问题菜单禁用此检查 (??)。”

I tried with changing the SQL dialect (because before there was also that message that appeared) without result.

我尝试更改 SQL 方言(因为之前也出现了该消息)但没有结果。

What should I do? I wrote something wrong in the code but I don't know why whit other example work perfectly.

我该怎么办?我在代码中写了一些错误,但我不知道为什么其他示例可以完美运行。

Here's the code:

这是代码:

public static void createTable(){

    //connect to database
    String url = percorsoDatabase;

    //Statement create new table
    String sql = "CREATE TABLE IF NOT EXISTS Tabella (id PRIMARY KEY," +
            "video TEXT," +
            "game TEXT," +
            "firstAction TEXT," +
            "secondAction TEXT," +
            "thirdAction TEXT);";

    try {
        Connection conn = DriverManager.getConnection(url);
        Statement stmt = conn.createStatement();
        stmt.execute(sql);
    } catch (SQLException e ){
            System.out.println(e.getMessage());
    }

}

I've already create a SQLite database and established a connection that works (before that), here's the code if it could be useful.

我已经创建了一个 SQLite 数据库并建立了一个有效的连接(在此之前),这是有用的代码。

public static void main(String[] args) {

    createNewDatabase();
    connection();
    createTable();
}

public static void createNewDatabase(){

    String url = percorsoDatabase;

    Connection conn;
    try {

     conn = DriverManager.getConnection(url);
        if (conn != null){
            DatabaseMetaData meta = conn.getMetaData();
            System.out.println("The driver name is" + meta.getDriverName());
            System.out.println("A new database has been created.");
        }
    } catch (SQLException e){
        System.out.println(e.getMessage());
    }

}

public static void connection(){

    Connection conn = null;
    try {
        //String url = "jdbc:sqlite://Volumes/Isma/Documenti/SUPSI/APA/Stage/"
        //        + "Beans/esperimento/dati.db";
        conn = DriverManager.getConnection(percorsoDatabase);
        System.out.println("Connection to SQLite established.");
    } catch (SQLException e){
        System.out.println(e.getMessage());
    } finally {
        try {
            if (conn != null){
                conn.close();
            }
        } catch (SQLException e){
            System.out.println(e.getMessage());
        }
    }

}

So... If you can help me I would be grateful.

所以......如果你能帮助我,我将不胜感激。

Thanks in advance for the answer and have a nice day!

提前感谢您的回答,祝您有美好的一天!

采纳答案by Tomasz Jagie??o

This warning indicates that you have not configured data source in Database Tool Window in Intellij Idea. It doesn't mean that your code is wrong, it just shows that you don't have code completion based on your database schema.

此警告表示您尚未在 Intellij Idea 的数据库工具窗口中配置数据源。这并不意味着您的代码是错误的,它只是表明您没有基于数据库架构的代码完成。

Configuration of datasource in Intellij is described here https://www.jetbrains.com/help/idea/database-tool-window.html

此处描述了 Intellij 中数据源的配置https://www.jetbrains.com/help/idea/database-tool-window.html

回答by Oli Z.

I think you should check the connection to your database.

我认为您应该检查与数据库的连接。

url - a database url of the form jdbc:subprotocol:subname

Check the Api https://docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.html#getConnection(java.lang.String)

检查 Api https://docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.html#getConnection(java.lang.String)

Did you add the ODBC correct?

您是否正确添加了 ODBC?

回答by valdeci

The message:

消息:

No data sources are configured to run this SQL and provide advanced code assistance.`

没有配置数据源来运行此 SQL 并提供高级代码帮助。`

is a little explicit and IMHO a little unnecessary, because I really think that I don't need to configure a database for every project which we have some SQL queries.

有点明确,恕我直言有点不必要,因为我真的认为我不需要为我们有一些 SQL 查询的每个项目配置一个数据库。

To handle with this message, you have two options:

要处理此消息,您有两个选择:

  1. Configure a Data Sourceto your project, using the ALT+ ENTERabove the message and using the option Configure a Data Source: Add a data source
  1. 配置数据源到您的项目,使用ALT+ENTER上述消息,并使用该选项配置数据源添加数据源

or

或者

  1. Disable this SQL dialect detectioninspection on the Inspection options on the IDE settings:

    Disable the SQL dialect detection

  1. 在 IDE 设置的检查选项上禁用此SQL 方言检测检查:

    禁用 SQL 方言检测

The second option is the best approach for me, unless you really want or need a Data Source in your project.

第二个选项对我来说是最好的方法,除非你真的想要或需要你的项目中的数据源。