0

Edit: The solution here worked for me: com.mysql.jdbc.Driver not found with mysql connector in buildpath

I don't understand why though. I already have the connector jar in my system classpath, why do I need to add it to the classpath in Eclipse as well? I'm glad that it's working, but I would like to understand. Is it just an Eclipse thing?

:end Edit

Sorry if this is already solved in another question. I've been looking around for quite a while and I haven't found the solution to my particular problem.

Discloser: This is for college, but connecting to the DB isn't the objective for the assignment. The objective is to understand the MVC setup.

I got the project war file from the GitHut here: https://github.com/Apress/beginning-jakarta-ee-web-dev/tree/master/ch05_code/eshop%20project

This seems to be written in an older style and possibly deprecated? It's using the web.xml file to assign some path Strings and things. This doesn't seem to work. So, I hard coded as much as I could find to bypass web.xml. I must still be missing something. I have added the tomcat jars and the connector jar to the build path. I'm stuck.

As far as I can tell, the issue is with one of these two files.

ShopServlet

    package eshop;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import eshop.model.DataManager;

@WebServlet(name = "ShopServlet", urlPatterns = {"/shop/*"})
public class ShopServlet extends javax.servlet.http.HttpServlet
    implements javax.servlet.Servlet {
  private static final long serialVersionUID = 1L;

  public ShopServlet() {
    super();
    }

  public void init(ServletConfig config) throws ServletException {
    System.out.println("*** initializing controller servlet.");
    super.init(config);

    DataManager dataManager = new DataManager();
    dataManager.setDbURL("jdbc:mysql://localhost:3306/shop");
    dataManager.setDbUserName("root");
    dataManager.setDbPassword("root");

    ServletContext context = config.getServletContext();
    context.setAttribute("base", "/eshop/shop");
    context.setAttribute("imageURL", "/eshop/images/");
    context.setAttribute("dataManager", dataManager);

    try {  // load the database JDBC driver
      Class.forName("com.mysql.cj.jdbc.Driver");
      }
    catch (ClassNotFoundException e) {
      System.out.println(e.toString());
      }
    }

  protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
    }

  protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    String base = "/jsp/";
    String url = base + "index.jsp";
    String action = request.getParameter("action");
    if (action != null) {
        switch (action) {
        case "search":
          url = base + "SearchOutcome.jsp";
          break;
        case "selectCatalog":
          url = base + "SelectCatalog.jsp";
          break;
        case "bookDetails":
          url = base + "BookDetails.jsp";
          break;
        case "checkOut":
          url = base + "Checkout.jsp";
          break;
        case "orderConfirmation":
          url = base + "OrderConfirmation.jsp";
          break;
        default:
          if (action.matches("(showCart|(add|update|delete)Item)"))
            url = base + "ShoppingCart.jsp";
          break;
        }
      }
    RequestDispatcher requestDispatcher =
      getServletContext().getRequestDispatcher(url);
    requestDispatcher.forward(request, response);
    }
  }

DataManager

package eshop.model;

import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Enumeration;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import eshop.beans.Category;
import eshop.beans.Book;
import eshop.beans.Customer;
import eshop.beans.CartItem;

public class DataManager {
  private String dbURL = "";
  private String dbUserName = "";
  private String dbPassword = "";

  public void setDbURL(String dbURL) {
    this.dbURL = dbURL;
    }
  public String getDbURL() {
    return dbURL;
    }

  public void setDbUserName(String dbUserName) {
    this.dbUserName = dbUserName;
    }
  public String getDbUserName() {
    return dbUserName;
    }

  public void setDbPassword(String dbPassword) {
    this.dbPassword = dbPassword;
    }
  public String getDbPassword() {
    return dbPassword;
    }

  public Connection getConnection() {
    Connection conn = null;
    try {
      conn = DriverManager.getConnection(getDbURL(), getDbUserName(),
          getDbPassword());
      }
    catch (SQLException e) {
      System.out.println("Could not connect to DB: " + e.getMessage());
      }
    return conn;
    }
  public void putConnection(Connection conn) {
    if (conn != null) {
      try { conn.close(); }
      catch (SQLException e) { }
      }
    }

  //---------- Category operations ----------
  public String getCategoryName(String categoryID) {
    Category category = CategoryPeer.getCategoryById(this, categoryID);
    return (category == null) ? null : category.getName();
    }

  public Hashtable<String, String> getCategories() {
    return CategoryPeer.getAllCategories(this);
    }

  public Enumeration<String> getCatIDs() {
    return CategoryPeer.getAllCategories(this).keys();
    }

  //---------- Book operations ----------
  public ArrayList<Book> getSearchResults(String keyword) {
    return BookPeer.searchBooks(this, keyword);
    }

  public ArrayList<Book> getBooksInCategory(String categoryID) {
    return BookPeer.getBooksByCategory(this, categoryID);
    }

  public Book getBookDetails(String bookID) {
    return BookPeer.getBookById(this, bookID);
    }

  //---------- Order operations ----------
  public long insertOrder(Customer customer,
            Hashtable<String, CartItem> shoppingCart) {
    long returnValue = 0L;
    long orderId = System.currentTimeMillis();
    Connection connection = getConnection();
    if (connection != null) {
      Statement stmt = null;
      try {
        connection.setAutoCommit(false);
        stmt = connection.createStatement();
        try {
          OrderPeer.insertOrder(stmt, orderId, customer);
          OrderDetailsPeer.insertOrderDetails(stmt, orderId, shoppingCart);
          try { stmt.close(); }
          finally { stmt = null; }
          connection.commit();
          returnValue = orderId;
          }
        catch (SQLException e) {
          System.out.println("Could not insert order: " + e.getMessage());
          e.printStackTrace();//MANELLI
          try { connection.rollback(); }
          catch (SQLException ee) { }
          }
        }
      catch (SQLException e) {
        System.out.println("Could not insert order: " + e.getMessage());
        e.printStackTrace();//MANELLI
        }
      finally {
        if (stmt != null) {
          try { stmt.close(); }
          catch (SQLException e) { }
          }
        putConnection(connection);
        }
      }
    return returnValue;
    }
  }

I'm getting an exception from line 49 in the DataManager file. "Could not connect to DB: No suitable driver found for ".

Any help would be appreciated. Is there a way to attach a war file so you guys can see the entire project?

  • The link to the question provided above worked for me. I don't know why though. I added the jar to my build path and it was already in my system classpath. Can anyone explain why this worked? Is it just an Eclipse thing? – Rusty DeGarmo Feb 13 '22 at 19:23

0 Answers0