【发布时间】:2016-11-15 18:20:02
【问题描述】:
我使用 IntelliJ IDEA 14.0、Tomcat 服务器和 h2 数据库来创建简单的 Web App。 不幸的是,当我运行应用程序时,我收到异常消息
java.lang.ClassNotFoundException: org.h2.Driver
我将 h2 jar 文件存储在名为“db”的文件夹中,并设置了“添加为库”功能。 我像这样连接到数据库:
public class DBConn {
private static final String DRIVER = "org.h2.Driver";
private static final String URL = "jdbc:h2:tcp://localhost/~/BigPicture";
private static final String USERNAME = "doncho";
private static final String PASS = "";
private static DBConn instance;
private static Connection conn;
private DBConn(){
}
public static DBConn getInstance(){
if(instance == null){
instance = new DBConn();
}
return instance;
}
public Connection getConnectivity(){
try {
Conn();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
private void Conn() throws SQLException{
if(conn == null){
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
System.out.println("No Driver Found");
e.printStackTrace();
}
DriverManager.getConnection(URL, USERNAME, PASS);
}
}
public void Disconnect(){
if(conn != null){
try {
conn.close();
conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
我在 Servlet 中调用数据库。
public class DBServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection conn = DBConn.getInstance().getConnectivity();
System.out.println("It Work's");
DBConn.getInstance().Disconnect();
}
输出显示“未找到驱动程序”并抛出 java.lang.ClassNotFoundException: org.h2.Driver.
重要的是,当我在Main 方法中调用类DBConn() 时,IntelliJ 找到h2 驱动程序,但Tomcat 仍然不能。
请帮忙,因为我是 IntelliJ 和 Eclipse 的新手,这个应用程序可以工作,但我想在我的项目中使用 IntelliJ。
最好的问候。
【问题讨论】: