0

I am trying to stablish connection with mysql database to servlet and here is my code. It is taking 20-25 seconds to compile befor showing the result on browser and it directly goes inside catch block. I can not understand what is the wrong I did. I checked database name, included mysql connector in library folder of eclipse IDE. Please need help.

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

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

@WebServlet("/registration")
public class Registration extends HttpServlet {
    private static final long serialVersionUID = 1L;
   
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        try {
            
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:1234/registration","root","4221");

            out.print("Database Updated");
        }catch(Exception e) {
            e.printStackTrace();
            out.print("Database is not updated");
        }
        out.close();
    }

}
  • 1
    If it takes so long to run, then it's probably a timeout. What does the exception message say? Also, did you make sure the database is running on the 1234 port? – Bogdan Dec 25 '20 at 14:09
  • Thank you very much @Bogdan. I have mistaken with the port number. The port number I mentioned over is set for tomcat server whereas the SQL server is using another one. I cross checked according to your comment and solved. Thanks again. – FOZLE RABBY Dec 25 '20 at 16:35

0 Answers0