我想在Eclipse中使用Servlet进行开发,但它说javax.servlet无法解析包.如何将javax.servlet包添加到Eclipse项目中?
你如何用Java连接到MySQL数据库?
当我尝试时,我明白了
java.sql.SQLException: No suitable driver found for jdbc:mysql://database/table
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
Run Code Online (Sandbox Code Playgroud)
要么
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)
要么
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
Run Code Online (Sandbox Code Playgroud) 我在Eclipse上设置了一个动态Web项目,我使用Tomcat 7作为我的Web服务器.它似乎没有自动将我添加到我的库中的第三方JAR放到我的构建路径上的WEB-INF/lib文件夹中.有没有办法可以自动完成?每次我寻找一个答案,我觉得像这样.
那我该怎么做呢?有没有办法配置我的构建路径来执行此操作?
我正在使用Eclipse 3.7(STS)和在IDE中运行的Tomcat 7.我创建了一个新的Dynamic Web项目,并将一个JSP文件添加到Web内容根文件夹中.我可以运行Tomcat并从Eclipse中访问JSP而没有任何问题.
我在用户库中为项目添加了一些第三方JAR(我没有使用maven或auto dependecies管理).在JSP中,我从项目的JAR文件中引用了一个类,我可以毫无问题地编译它,但是当我在Tomcat上部署时,JSP会抛出ClassNotFoundException.显然,Tomcat无法从我的库设置中找到JAR.我尝试为Tomcat Server创建Run As配置,并设置classpath以匹配项目的类路径设置,但我仍然得到相同的classnotfound问题.
我可以通过手动将所有项目JAR复制到WEB-INF/lib目录来解决这个问题,因此webapp可以找到所有依赖项,但这是荒谬的,我不认为这是解决方案,因为它是维护的噩梦.
我错过了什么吗?
我正在使用Eclipse,我需要能够将Java库(JAR文件)添加到我的Web应用程序的WEB-INF/lib文件夹中.我该如何实现这一目标?
我使用下面的代码将文件上传到tomcat5.5,它给了我以下异常
java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
你能帮我找一下吗?
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class FileUploadServlet
*/
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FileUploadServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
@SuppressWarnings("rawtypes")
protected void doGet(HttpServletRequest request, HttpServletResponse response) …Run Code Online (Sandbox Code Playgroud) 当我在eclipse Dynamic Web Project里面使用follow代码时servlet,像这样:
@WebServlet("/CreateCustomerServlet")
public class CreateCustomerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private void test() throws SQLException, ClassNotFoundException {
// use the JAR ...
Class.forName("com.mysql.jdbc.Driver");//here the exception
}
}
Run Code Online (Sandbox Code Playgroud)
抛出异常:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
在线:
Class.forName("com.mysql.jdbc.Driver");
在动态Web项目的Build路径中,我添加了jar:
mysql-connector-java-5.1.21-bin.jar
我添加了文件夹的快照,你可以看到它所需的jar -

我正在使用Eclipse IDE并正在编写一个servlet.servlet应该接受来自html文件的值并相应地返回JSON响应.
我的doPost()是:
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
try
{
res.setContentType("application/json");
res.setHeader("Cache-Control", "nocache");
res.setCharacterEncoding("utf-8");
PrintWriter out = res.getWriter();
JSONObject json = new JSONObject();
String un=req.getParameter("uname");
String pw=req.getParameter("password");
if(un.equals("xxx") && pw.equals("xxx"))
json.put("result", "success");
else
json.put("result", "fail");
out.print(json.toString());
}
catch(Exception e){System.out.print( e.getMessage());}
}
Run Code Online (Sandbox Code Playgroud)
当我在Eclipse中运行这个servlet时,我得到一个文件下载对话框.
当使用Tomcat在Eclipse外部运行时,我收到错误:
root cause
java.lang.NoClassDefFoundError: org/json/JSONObject
Server1.doPost(Server1.java:25)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
java.lang.ClassNotFoundException: org.json.JSONObject
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
Server1.doPost(Server1.java:25)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
Run Code Online (Sandbox Code Playgroud)
Server1.doPost(Server1.java:25)行引用
JSONObject json = new JSONObject();
Run Code Online (Sandbox Code Playgroud)
我已将org.json.jar添加到构建路径中,并将其添加到部署路径中 Properties->Configure build path->Deployment assembly
有一个非常类似的问题,但在我的情况下,我的构建路径中没有任何重复的jar,所以解决方案对我不起作用.我现在已经搜索了谷歌几个小时,但我找到的解决方案都没有解决我的问题.我正在创建一个带有一些数据库连接的网站来完成作业.我正在使用MySQL数据库,在Eclipse中开发并在Windows上运行.
我一直在java.lang.ClassNotFoundException: com.mysql.jdbc.Driver使用以下代码:
import java.sql.*;
//...
public void someMethodInMyServlet(PrintWriter out)
{
Connection connection = null;
PreparedStatement query = null;
try {
out.println("Create the driver instance.<br>");
Class.forName("com.mysql.jdbc.Driver").newInstance();
out.println("Get the connection.<br>");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "secret");
query = connection.prepareStatement( "SELECT * FROM customers");
//...
} catch (Exception e)
{
out.println(e.toString()+"<br>");
}
}
//...
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,我得到以下输出:
Create the driver instance.
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)
它没有超越Class.forName...线,我无法弄清楚为什么!这是我做的:
C:\Program Files\MySQL\mysql-connector-java-5.1.12\mysql-connector-java-5.1.12-bin.jar. mysql-connector-java-5.1.12-bin.jar.每当我尝试使用servlet时,无论我是否有jar或者我没有,我都会得到相同的错误.你能帮我搞清楚吗?
我试图在Windows7上使用java连接到mysql数据库.尽管在CLASSPATH中添加了jdbcdriver jar文件的完整url,但抛出了java.lang.ClassNotFoundException:com.mysql.jdbc.Driver.谁能告诉我我在这里失踪了什么?如果我在项目库中添加jar文件但是我想通过CLASSPATH本身来实现它.我的类路径看起来像这样 - C:\ jython2.5.1\javalib\mysql-connector-java-5.1.12-bin.jar
我想说清楚这不是我正在进行的实际项目.我实际上正在使用Django和Jython,它需要JDBC驱动程序来访问数据库.这就是为什么我必须只使用CLASSPATH.