我正在尝试使用H2连接到 Java 中的数据库(使用 Eclipse 作为 IDE)。该示例确实(如下)抛出了一个ClassNotFoundException
. 问题是,我确实将 h2 jar 文件添加到系统 CLASSPATH 中。printenv
我什至通过控制台多次检查了它的存在。我省略了一步吗?
代码:
import java.sql.*;
public class Program {
/**
* @param args
*/
public static void main(String[] args)
throws Exception{
try{
System.out.println("hello, world!");
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:~/testdb", "sa", "");
// add application code here
conn.close();
}catch(ClassNotFoundException ex){
System.out.println( "ERROR: Class not found: " + ex.getMessage() );
}
System.exit(0);
}
}