Home > Back-end >  failed connection Servlet error java and MySql
failed connection Servlet error java and MySql

Time:01-09

today i have a problem with servlet in java and mySql, btw i'm using eclipse to write this code. The problem is that when i use the servlet to call my DAO class in tomcat, my connection to the database fails, but when i use a normal class to make a try with java compiler, the connection is good, this error is only when i use Tomcat.

this is my Servlet controller (in this the connection fails and i run this with 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);
    }
}

This is my DAO class

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

This is the error message

enter image description here

This is my Try controller

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

And this is when i use the try controller

enter image description here

like i said before, in this try i am using java compiler of eclipse and i don't have the problem but, when i use tomcat again in this controller my system failes, i dont know how can i resolve it

CodePudding user response:

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

You should look at this page to install mysql driver properly in Tomcat

  • Related