简体   繁体   English

Apache Tomcat上的Servlet和JAR

[英]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. 我目前正在尝试获取一个servlet来执行SQL查询并在网页上返回结果。 I am using SQLite and the JAR has been added to my project class path. 我正在使用SQLite,并且JAR已添加到我的项目类路径中。 However, Eclipse informs me that the code: 但是,Eclipse告诉我代码:

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

has a "ClassNotFoundException" 有一个“ ClassNotFoundException”

When I run the code the server gives me a message about the ClassNotFoundException. 当我运行代码时,服务器会向我发送有关ClassNotFoundException的消息。

When I first looked up help I read that I should put the JAR into the "lib" folder of Apache, which I did. 当我第一次查找帮助时,我读到我应该将JAR放到Apache的“ lib”文件夹中。

I also clicked on the Servers in my Eclipse Project, clicked "Profile As -> Profile Configurations" and added the JAR to the Classpath as well. 我还单击了Eclipse Project中的服务器,单击了“ Profile As-> Profile Configurations”,并将JAR也添加到了Classpath中。

I also tried adding the JAR to the WEB-inf/lib as well as another answer suggested, but the problem still occurs. 我还尝试将JAR添加到WEB-inf / lib以及建议的另一个答案,但是问题仍然存在。

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. JAR必须位于服务器上的某个位置才有意义,以便servlet可以找到该类,我只是不知道如何解决该问题。

"I also tried adding the JAR to the WEB-inf/lib as well as another answer suggested, but the problem still occurs." “我还尝试将JAR添加到WEB-inf / lib以及建议的另一个答案,但问题仍然存在。” That is where the JAR should physiclly be. 那应该是JAR的物理位置。 Then go properties> build path > add JARS and add the JAR 然后去属性>构建路径>添加JAR并添加JAR

If you are using eclispe you dont need to setup by yourself paths 如果您使用的是eclispe,则无需自己设置路径

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 您需要将jdbc驱动程序添加到正确的文件夹,然后按正确的文件夹(我的意思是(WebContent-> WEB-INF-> lib)),而无需使用eclipse

find your workspace and pase the driver right there, after this refresh(F5) your project in eclpise and it should be there 在刷新(F5)之后,在eclpise中找到您的项目,它应该在那里

to be sure, create new DynamicWebProject and paste this code 确保,创建新的DynamicWebProject并粘贴此代码

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

} }

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

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