javamysqleclipsejdbctomcat7

JBDC driver not found even though I have added it to the classpath


I'm having a problem with my Dynamic Web Project. This is my first project, and I want to do something fairly simple. I want to print out the data from my database. I use MYSQL and have set up a database. I want to connect to a database by using the following code:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    // Set the MIME type for the response message
      response.setContentType("text/html");
      // Get a output writer to write the response message into the network socket
      PrintWriter out = response.getWriter();
      
      Connection conn = null;
      Statement stmt = null;
      try {
         // Step 1: Create a database "Connection" object
         // For MySQL
         conn = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/webshop", "root", "admin");  // <<== Check

        
      } catch (SQLException ex) {
         ex.printStackTrace();
      }
    }

I get the same message as other posts have already mentioned:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/webshop

I understood from other posts that one must add the connector to the classpath. I have done this by using the properties wizard of Eclipse (the IDE I'm using). This did not work! I have added the mysql-connector-java-5.1.37 to the classpath. I also tried adding it directly to the WEB-INF/lib folder, alas with no success. The last thing I tried, also unsuccessfully, was to add it to the Tomcat 7 library.

None of the above seems to work. What am I doing wrong? I couldn't find a working suggestion in the other posts.


Solution

  • Thanks for all the great tips and help! Most of the options I found here, I had already tried. So to me it seemed like something was wrong for my setup in particular. It was not a general issue.

    I have resolved the issue by simple starting over. Delete eclipse, the tomcat server, everything... Afterwards install again all the necessary stuff. This time it worked first try! I added the connector (only) to the /lib of the tomcat server.

    I think maybe the issue was that I had multiple connectors somewhere in the project? Anyway, starting over resolved my issue. I know it's not a nice clean fix, but it worked.

    Thanks again for the effort and time!