No data sources are configured to run this SQL

2020-06-12 12:19发布

问题:

I have a little problem with the creation of a table (for a database) in 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:

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

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

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.

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!

回答1:

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.

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



回答2:

The message:

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

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.

To handle with this message, you have two options:

  1. Configure a Data Source to your project, using the ALT + ENTER above the message and using the option Configure a Data Source:

or

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

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



回答3:

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)

Did you add the ODBC correct?