JntelliJ找不到司机班

时间:2016-11-15 18:20:02

标签: java tomcat driver h2

我使用IntelliJ IDEA 14.0,Tomcat服务器和h2数据库来创建简单的Web App。 不幸的是,当我运行应用程序时,我收到异常消息

  

java.lang.ClassNotFoundException:org.h2.Driver

img

我将h2 jar文件存储在名为“db”的文件夹中,并设置“Add as library”功能。 我像这样连接到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.

这是重要的,当我在DBConn方法IntelliJ fInd Main驱动程序中调用类h2()时,但Tomcat仍然不能。

请帮忙,因为我是IntelliJ和Eclipse的新手这个应用程序工作,但我想将IntelliJ用于我的项目。

最好的问候。

1 个答案:

答案 0 :(得分:0)

将驱动程序的jar文件放在$ TOMCAT_HOME / lib或yourapp / WEB-INF / lib