Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No suitable driver found" while conneting JavaFX (Maven) project with H2 Embedded Database

Tags:

maven

javafx

h2

I'm having a little problem making simple CRUD application in JavaFX using Maven and H2 Database. I've created a database (which .db file is located at Users direction by default) and I've problem with connected my Java project with this file. My database file (data.mv.db) is located in default directory so URL of JBDC is absolutely correct.

It looks like the DBConnection class isn't seeing my .db file, I don't know why.

When I'm running a DBConnection class IDE's putting out SQLException:

java.sql.SQLException: No suitable driver found for jdbc:h2:~/data

Here is my DBConnection class code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection {
    public static void main(String[] args) {
        String jdbcURL = "jdbc:h2:~/data";
        String username = "sa";
        String password = "123";

        try {
            Connection connection = DriverManager.getConnection(jdbcURL, username, password);
            System.out.println("Connected to H2 embedded database.");

            connection.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

H2 Database library is added to my project by dependency in Maven. Here is dependency in pom.xml file:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>2.2.220</version>
    <scope>test</scope>
</dependency>
like image 288
Damian Avatar asked Dec 15 '25 06:12

Damian


1 Answers

Your dependency is for test scope, so it is not added to the runtime classpath.

You need to change it to runtime. Change <scope>test</scope> to <scope>runtime</scope>.

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>2.2.220</version>
    <scope>runtime</scope>
</dependency>

The scope element can be omitted if it is a compile dependency, because the scope defaults to that. However, if that's the case, the error would be a compile time and not at runtime.

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>2.2.220</version>
</dependency>

See also:

  • Dependency Scope in Apache Maven documentation.
  • What is <scope> under <dependency> in pom.xml for?
like image 195
Andrés Alcarraz Avatar answered Dec 16 '25 23:12

Andrés Alcarraz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!