简体   繁体   中英

Servlet and JAR on Apache Tomcat

I'm currently trying to get a servlet to perform a SQL query and return the results on a web page. I am using SQLite and the JAR has been added to my project class path. However, Eclipse informs me that the code:

Class.forName("org.sqlite.JDBC");

has a "ClassNotFoundException"

When I run the code the server gives me a message about the ClassNotFoundException.

When I first looked up help I read that I should put the JAR into the "lib" folder of Apache, which I did.

I also clicked on the Servers in my Eclipse Project, clicked "Profile As -> Profile Configurations" and added the JAR to the Classpath as well.

I also tried adding the JAR to the WEB-inf/lib as well as another answer suggested, but the problem still occurs.

It makes sense that the JAR needs to be on the server somewhere so that the servlet can find the class, I just don't know how to fix the problem.

"I also tried adding the JAR to the WEB-inf/lib as well as another answer suggested, but the problem still occurs." That is where the JAR should physiclly be. Then go properties> build path > add JARS and add the JAR

If you are using eclispe you dont need to setup by yourself paths

you need to add jdbc driver to right folder and then by right folder I mean (WebContent -> WEB-INF -> lib) you can make this without using eclipse

find your workspace and pase the driver right there, after this refresh(F5) your project in eclpise and it should be there

to be sure, create new DynamicWebProject and paste this code

import java.sql.*;

public class SQLiteJDBC {
public static void main(String args[]) {
    Connection conn = null;
    Statement stat = null;
    try {
        Class.forName("org.sqlite.JDBC");
        conn = DriverManager.getConnection("jdbc:sqlite:test.db");
        conn.setAutoCommit(false);
        System.out.println("sqlite opened successfully");

        stat = conn.createStatement();
        //  sql is the query check it online on sqlite page or sql queries
        String sql = "INSERT INTO 'name of db and parameters to eqecute'";
        stat.executeUpdate(sql);

        stat.close();
        conn.commit();
        conn.close();
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }
}

}

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