How to connect MySQL to a servlet?

@WebServlet(name = "ServletBlog", mapping includes = "/posts")
public class Servlets extends HttpServlet {
 try {
 catch (ClassNotFoundException e) {
 // TODO Auto-generated catch block
 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");

in the Classpath loaded driver(in intellij idea(mysql-connector-java-5.1.39-bin.jar)) but still says "DB CONNECT ERROR"
July 8th 19 at 15:38
4 answers
July 8th 19 at 15:42
You never know. Maybe the password does not match. Read the logs carefully.
all come already tested more than once - roger28 commented on July 8th 19 at 15:45
July 8th 19 at 15:40
Found the answer:

1.If you're running inside an IDE like Eclipse or IntelliJ, you have to add the JAR to a library.
2.IF you're running in a command shell, use the -p option for javac.exe when you compile and java.exe when you run.
3.If you're using it in a web app, you can start by putting it in the WEB-INF/lib directory of your WAR file. If you're using a servlet/JSP engine like Tomcat 6, put it in the Tomcat /lib directory.

helped me paragraph 3.
July 8th 19 at 15:44
Check whether jarnik was in CP and drag and drop the Class.forName method doCet before doing getConnection. If does not help, then the XS even.
: helps - roger28 commented on July 8th 19 at 15:47
July 8th 19 at 15:46
The artifacts and try to create the libs folder for example, and attach a previously created instance of the libraries with the mysql drivers

Find more questions by tags JavaMySQLJava EEJDBC