@WebServlet(name = "ServletBlog", urlPatterns = "/posts") public class Servlets extends HttpServlet { { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3300/db","root","root"); PreparedStatement preparedStatement = null; ResultSet resultSet = preparedStatement.executeQuery("SELECT * FROM db")) { } catch (SQLException e) { System.err.println("DB CONNECT ERROR"); } 

loaded driver in Classpath (in intellij idea (mysql-connector-java-5.1.39-bin.jar)) but still writes "DB CONNECT ERROR"

example exception:

 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3300/db 
  • The question is not correct: MySQL and servlet you “connected”, but the real problem is that your code does not see the required class. Perhaps you need to specify in your project a dependency on the external library: nixmash.com/java/… . - slava

1 answer 1

Found the answer:

1.If you are running inside an IDE like Eclipse or IntelliJ, you have to add the JAR to a library.

2.IF you are running in a command shell, use the j option for the javac.exe when you run.

3.If you’re using a web app, you can’t get it. If you’re using a servlet / JSP engine like Tomcat 6, put it in the Tomcat / lib directory.

point 3 helped me.