Reputation:
I am working on a dB viewer for a h2 database in java.
I am using a sample code to try and connect to it as follows:
import java.sql.*;
public class db {
public static void main(String[] a)
throws Exception {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.
getConnection("jdbc:h2:~/test", "sa", "");
// add application code here
conn.close();
}
}
But when I run it I get the following error:
Exception in thread "main" java.lang.ClassNotFoundException: org.h2.Driver at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at db.main(db.java:5)
Any ideas? I have installed h2 using the installer.
Upvotes: 0
Views: 3311
Reputation: 804
Simply, you have not correctly added the h2 database as a dependency of your Java project.
Rightclick on Project > Properties > Java Build Path > Add External Jars
. Select the h2 database library JAR file.Upvotes: 0
Reputation: 1735
Add the h2 jdbc driver to your classpath or as library to your project if you using an IDE.
Upvotes: 0