连接失败 Servlet 错误 java 和 MySql

huangapple go评论75阅读模式
英文:

failed connection Servlet error java and MySql

问题

今天我在Java和MySQL中遇到了一个Servlet的问题,顺便说一下,我在使用eclipse编写这段代码。问题是当我在Tomcat中使用Servlet调用我的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.getConnection().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; 
        }
    }
}

这是错误信息

这里是错误信息的图片

这是我的尝试控制器

public class Controller{

    public static void main(String[] args) {
        Connection_ jdbc = new Connection();
        Statement statement;
        ResultSet rows;
        try {
            statement = jdbc.getConnection().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");
        }
    }
}

这是当我使用尝试控制器时的情况

在这里是当我使用尝试控制器时的图片

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

英文:

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(&quot;accion&quot;);
	
	if(accion == null || accion.isEmpty()) {
		dispatcher = request.getRequestDispatcher(&quot;products/index.jsp&quot;);
		List&lt;Product&gt; list = productDAO.getList();
		request.setAttribute(&quot;list&quot;, 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&lt;Product&gt; getList() {
	Connection_ jdbc = new Connection();
	PreparedStatement ps;
	ResultSet rs;
	List&lt;Product&gt; list = new ArrayList&lt;&gt;();
	
	try {
            ps = jdbc.getConecction().prepareStatement(&quot;SELECT * FROM products&quot;);;
            rs = ps.executeQuery();
            while(rs.next()) {
		list.add(new Product(rs.getInt(&quot;id&quot;), rs.getInt(&quot;stock&quot;), rs.getDouble(&quot;price&quot;), rs.getString(&quot;name&quot;), rs.getString(&quot;code&quot;)));
            }
            return list;
	}catch (Exception e) {
            System.out.println(&quot;Fallo en la conexion&quot;);
            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(&quot;SELECT * FROM products&quot;);
            System.out.println(&quot;Registers&quot;);
            while(rows.next()) {
                System.out.println(&quot;Id &quot;+rows.getInt(&quot;stock&quot;));
            }
	} catch (Exception e) {
            System.out.println(&quot;Fallo en la conexion&quot;);
	}
}

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

答案1

得分: 1

你应该查看此页面以正确在Tomcat中安装MySQL驱动程序。

英文:

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

huangapple
  • 本文由 发表于 2023年1月9日 04:21:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定