0

I'm trying to connect to my embedded H2 database via Java. I found various threads and tutorials on this and now have this code:

Connection con = null;
Properties connectionProps = new Properties();
connectionProps.put("user", "username");
connectionProps.put("password", "password");
try {
    Class.forName("org.h2.Driver");
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

con = DriverManager.getConnection("jdbc:h2:~/test", connectionProps);

I got the "no suitable driver found for jdbc:h2:~/test" error message. I found the Class.forName(...) in some threads as a solution to this, but it doesn't seem to be working (ClassNotFoundException). What exactly do I need to do to get this working?

Edit: Put the correct H2 driver in Class.forname(); I still get the same exceptions and the post that was linked in the closing notice doesn't have an answer either (actually has the same problem).

Nobody
  • 45
  • 6
  • 1
    Do you have the H2 database driver on the classpath? I notice that you are trying to load the MySQL JDBC driver, that is of course not relevant when you want to use H2 (although with automatic driver loading, if the H2 driver is on the class path, it would work in a simple Java application). Please provide a [mre], including how you run your application. – Mark Rotteveel Jun 29 '20 at 15:34
  • 1
    Why are you using the driver class and URL syntax for MySQL if you want to use H2? –  Jun 29 '20 at 15:38
  • fixed it to `Class.forName("org.h2.Driver");` Still get the classnotfoundexception – Nobody Jun 29 '20 at 15:46
  • Dump your classpath to the console to see if it contains H2. – Basil Bourque Jun 29 '20 at 15:48
  • How do I do that? – Nobody Jun 29 '20 at 16:08
  • You define the classpath when you run your application. How are you running your application? – Mark Rotteveel Jun 29 '20 at 17:08

1 Answers1

1

You are using completely different drivers

Class.forName("com.mysql.jdbc.Driver"); this is mysql driver

jdbc:h2:~/test this is h2 db

You need to call Class.forName("org.h2.Driver");

More info is here: https://superuser.com/questions/290999/where-can-i-find-h2-jdbc-driver

Antaaaa
  • 233
  • 3
  • 14
  • I think the problem is that the OP doesn't have the driver on the classpath. With automatic driverloading, using `Class.forName` hasn't been necessary since 2006 (at least in simple Java applications, complex applications with multiple classloaders and classpaths are a different story). – Mark Rotteveel Jun 29 '20 at 15:37
  • I just tried `Class.forName("org.h2.Driver");` but get the same ClassNotFoundException – Nobody Jun 29 '20 at 15:41
  • @MarkRotteveel I've been using JDBC thru Class.forName and in all books that I've read authors was doing the same. Need to read about this, do you have some links? – Antaaaa Jun 29 '20 at 15:42
  • Sure, read the [JDBC 4.3 specification](https://jcp.org/aboutJava/communityprocess/mrel/jsr221/index3.html), specifically sections 4.1 and 9.2, [`DriverManager`](https://docs.oracle.com/en/java/javase/11/docs/api/java.sql/java/sql/DriverManager.html), or look at [How is driver class located in JDBC4](https://stackoverflow.com/questions/18288058/how-is-driver-class-located-in-jdbc4). As I said, it hasn't been necessary since 2006 with Java 6 (JDBC 4) in simple Java programs, assuming a JDBC 4 or higher driver is used. – Mark Rotteveel Jun 29 '20 at 17:01
  • @MarkRotteveel - Thanks a lot! – Antaaaa Jun 29 '20 at 17:06