简体   繁体   English

如何使用Tomcat和MySQL配置Eclipse

[英]How to configure Eclipse with Tomcat and MySQL

I am trying to get rows from a MySQL table with JSP in Eclipse. 我试图在Eclipse中使用JSP从MySQL表中获取行。 My Tomcat server is started, jConnector is in JRE Library and my code is: 我的Tomcat服务器已启动,jConnector在JRE库中,我的代码是:

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {
    public Connection getDBConnection() throws Exception{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection  conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","myuname","mypass");
        return conn;
    }
}

JSP is : JSP是:

<jsp:useBean id="dbConn" scope="request" class="dbConn.DBConnection"/>
<%
  Connection conn=dbConn.getDBConnection();
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select * from user");
%>
<html>
...
<body>
<%while (rs.next()){ %>
<h1><%=rs.getString(2) %></h1>
<%} %>
</body>
</html>
<%
  if(conn!=null)
    conn.close();
%>

And final result is 最后的结果是

error: root cause 错误:根本原因

javax.servlet.ServletException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver javax.servlet.ServletException:java.lang.ClassNotFoundException:com.mysql.jdbc.Driver

jConnector is in JRE Library jConnector位于JRE库中

This is not correct. 这是不正确的。 You should drop the JAR file in web project's /WEB-INF/lib folder. 您应该将JAR文件放在Web项目的/WEB-INF/lib文件夹中。 That should be basically all you need to do. 这基本上应该是你需要做的。 Everything in this folder ends up in webapp's runtime classpath. 此文件夹中的所有内容都以webapp的运行时类路径结束。 If you have also fiddled around in project's Build Path properties, then I recommend to undo that all. 如果您还在项目的构建路径属性中摆弄,那么我建议撤消所有这些。 It would otherwise possibly break or confuse something else now or in the future. 否则,现在或将来可能会破坏或混淆其他东西。

See also: 也可以看看:


Unrelated to the concrete problem: writing Java code in a JSP file is strongly discouraged since a decade. 具体问题无关 :十年来,强烈建议不要在JSP文件中编写Java代码。 I recommend to write Java code in normal Java classes and learn about servlets to control the HTTP requests. 我建议在普通的Java类中编写Java代码,并了解servlet以控制HTTP请求。

See also: 也可以看看:

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

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