Search code examples
javasqliteintellij-idea

No data sources are configured to run this SQL


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!


Solution

  • 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