Reputation: 19846
I am trying an example of using H2. But I cannot create an in-memory DB. When I run the following program, I just get an error message:
java.sql.SQLException: No suitable driver found for jdbc:h2:mem at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702) at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251) at db.DBExample.main(DBExample.java:14)
In pom.xml I included
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>test</scope>
</dependency>
Anybody knows what is wrong?
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DBExample {
public static void main(String[] a) throws Exception {
var url = "jdbc:h2:mem:";
try (var con = DriverManager.getConnection(url);
var stm = con.createStatement();
var rs = stm.executeQuery("SELECT 1+1")) {
if (rs.next()) {
System.out.println(rs.getInt(1));
}
} catch (SQLException ex) {
var lgr = Logger.getLogger(DBExample.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
}
}
Upvotes: 2
Views: 2975
Reputation: 1123
The problem is because you have specified <scope>test</scope>
.
Remove the scope line.
This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
Have a look here : driver not found
Upvotes: 5