英文:
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("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
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
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论