简体   繁体   中英

JntelliJ can't find driver class

I used IntelliJ IDEA 14.0, Tomcat server and h2 database to create simple Web App. Unfortunately when I run the application I receive message for exception

java.lang.ClassNotFoundException: org.h2.Driver

IMG

I stored h2 jar file in folder with name "db" and set "Add as library" function. 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.

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.

It's importand to say, that when I call class DBConn () in Main method IntelliJ fInd h2 driver, but Tomcat still can't.

Please help because I'm new in IntelliJ and in Eclipse this app work, but I want to use IntelliJ for my projects.

Best regards.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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