简体   繁体   English

JntelliJ找不到驱动程序类

[英]JntelliJ can't find driver class

I used IntelliJ IDEA 14.0, Tomcat server and h2 database to create simple Web App. 我使用IntelliJ IDEA 14.0,Tomcat服务器和h2数据库来创建简单的Web App。 Unfortunately when I run the application I receive message for exception 不幸的是,当我运行该应用程序时,我收到异常消息

java.lang.ClassNotFoundException: org.h2.Driver java.lang.ClassNotFoundException:org.h2.Driver

IMG

I stored h2 jar file in folder with name "db" and set "Add as library" function. 我将h2 jar文件存储在名为“ db”的文件夹中,并设置了“添加为库”功能。 I connect to the DB like this: 我这样连接到数据库:

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();
        }
    }
}

} }

And I call database in Servlet. 我在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();
}

The Output says "No Driver Found" and throws java.lang.ClassNotFoundException: org.h2.Driver. 输出显示“未找到驱动程序”,并引发java.lang.ClassNotFoundException: org.h2.Driver.

It's importand to say, that when I call class DBConn () in Main method IntelliJ fInd h2 driver, but Tomcat still can't. 重要的是,当我在Main方法IntelliJ fInd h2驱动程序中调用类DBConn ()时,但是Tomcat仍然不能。

Please help because I'm new in IntelliJ and in Eclipse this app work, but I want to use IntelliJ for my projects. 请帮忙,因为我是IntelliJ和Eclipse的新用户,但我想将IntelliJ用于我的项目。

Best regards. 最好的祝福。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM