0

I've tried looking at every answer related to this question and it still gives me the same error. I've tried including jar files to my project and it still doesn't work. Please help me. This is my servlet file.

package com.test.testdb;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestDbServlet
 */
@WebServlet("/TestDbServlet")
public class TestDbServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, 
       HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse 
response) throws ServletException, IOException {
        String user = "test";
        String pass = "test";

        String jdbcUrl = "jdbc:mysql://localhost:3306/test? 
   useSSL=false";
        String driver = "com.mysql.jdbc.Driver";

        try {
            PrintWriter out = response.getWriter();
            out.println("Connecting to database: " + jdbcUrl);
            Class.forName(driver);
            Connection myConn = DriverManager.getConnection(jdbcUrl, user, pass);
            out.println("yay");
            myConn.close();
        }
        catch (Exception exc) {
            exc.printStackTrace();
            throw new ServletException(exc);
        }
    }

}

I've already added mysql-connector-java-5.1.47-bin.jar to my WEB-INF>lib folder. I'm new to this so I'm still trying to figure things out. Please help me. This is what I get when I run the file.

Connecting to database: jdbc:mysql:localhost:3306/test java.sql.SQLException: No suitable driver found for >jdbc:mysql:localhost:3306/test at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.hibernate_tutorial.jdbc.Driver.main(Driver.java:16)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
trumanblack1025
  • 471
  • 2
  • 8
  • 19
  • https://stackoverflow.com/questions/5556664/how-to-fix-no-suitable-driver-found-for-jdbcmysql-localhost-dbname-error-w – Ng Sharma Sep 09 '18 at 08:40
  • 1
    The exception message and your code don't match. The error suggest you use the URL `>jdbc:mysql:localhost:3306/test` which doesn't match the URL in your code. The URL in the exception message is clearly wrong because of the `>` and the messing `//` after `jdbc:mysql:`. For more information, see the duplicate. – Mark Rotteveel Sep 09 '18 at 12:20
  • @StephenC Given the `Class.forName` doesn't yield a `ClassNotFoundException`, the driver is present, meaning the problem is more likely one of using the wrong URL. – Mark Rotteveel Sep 09 '18 at 12:22
  • Good point. I hadn't noticed that. – Stephen C Sep 09 '18 at 12:23

0 Answers0