连接失败Servlet错误java和MySql



今天我在java和mySql中遇到servlet问题,顺便说一句,我使用eclipse编写此代码。问题是,当我使用servlet在tomcat中调用我的DAO类时,我与数据库的连接失败,但当我使用正常类与java编译器进行尝试时,连接良好,此错误仅在我使用tomcat时出现。

这是我的Servlet控制器(在这个连接失败,我运行这个与tomcat)

public class ProductController extends HttpServlet {
private static final long serialVersionUID = 1L;
public ProductController() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ProductDAO productDAO = new ProductDAO();
RequestDispatcher dispatcher = null;
String accion = request.getParameter("accion");

if(accion == null || accion.isEmpty()) {
dispatcher = request.getRequestDispatcher("products/index.jsp");
List<Product> list = productDAO.getList();
request.setAttribute("list", list);
}
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
这是我的DAO类
public class ProductDAO {
public ProductDAO() {
}
public List<Product> getList() {
Connection_ jdbc = new Connection();
PreparedStatement ps;
ResultSet rs;
List<Product> list = new ArrayList<>();

try {
ps = jdbc.getConecction().prepareStatement("SELECT * FROM products");;
rs = ps.executeQuery();
while(rs.next()) {
list.add(new Product(rs.getInt("id"), rs.getInt("stock"), rs.getDouble("price"), rs.getString("name"), rs.getString("code")));
}
return list;
}catch (Exception e) {
System.out.println("Fallo en la conexion");
return null; 
}
}
}

这是错误信息

输入图片描述

这是我的Try控制器

public class Controller{
public static void main(String[] args) {
Connection_ jdbc = new Connection();
Statement statement;
ResultSet rows;
try {
statement = jdbc.getConecction().createStatement();
rows = statement.executeQuery("SELECT * FROM products");
System.out.println("Registers");
while(rows.next()) {
System.out.println("Id "+rows.getInt("stock"));
}
} catch (Exception e) {
System.out.println("Fallo en la conexion");
}
}

这是我使用try控制器

的时候输入图片描述

就像我之前说的,在这个尝试中,我使用eclipse的Java编译器,我没有问题,但是,当我在这个控制器中再次使用tomcat时,我的系统失败了,我不知道如何解决它

https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-tomcat.html

你应该看看这个页面来正确安装mysql驱动程序在Tomcat

相关内容

  • 没有找到相关文章

最新更新