0

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.

1
  • you need to add jar that contains org.h2.Driver class in your classpath. Commented Apr 13, 2015 at 16:16

2 Answers 2

0

Add the h2 jdbc driver to your classpath or as library to your project if you using an IDE.

Sign up to request clarification or add additional context in comments.

Comments

0

Simply, you have not correctly added the h2 database as a dependency of your Java project.

  • If you're using Eclipse you may use Rightclick on Project > Properties > Java Build Path > Add External Jars. Select the h2 database library JAR file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.