Recently, I had to work on Eclipse for writing Servlet and JSP programs which connects to the MySQL database. I want to configure Eclipse to use Apache Tomcat as server and connects to MySQL database using Servlet program so, I googled it to find what to do as many of them does, but I haven’t found any resource which provides straight forward guide to fulfill my requirement.

After doing some experimental things i have understood the procedure of configuring it so thought of putting a post about it. By the end of the following steps you will be able to configure your eclipse to run Servlet and JSP programs to connect to the MySQL database.

  1. Install MySQL Database.
  2. Install Apache Tomcat.
  3. Install Eclipse IDE for Java EE Developers.
  4. Check whether MySQL is properly installed and running on the system by following below steps:
    • Open terminal or command prompt.
    • Enter “mysql -u root -p” (Here the username is root).
    • Enter the password when prompted.
    • Now create a database using “CREATE DATABASE EXAMPLE”(Here EXAMPLE is the name of database).
    • Select the database using “USE EXAMPLE”.
    • Create the tables according the required schema.

  5. Check whether Apache Tomcat is properly installed and running on the system by following below steps:
    • Type http://localhost:8080 in browser’s location bar.
    • The browser should display a welcome home page of Apache Tomcat, if its not then:
               Start the Apache Tomcat Service Runner and try again, or check whether port 8080 is the one on which the Tomcat is running.
  6. Select the connector for MySQL  Database :
             I have used the Thin Driver which is mysql-connector-java-5.1.12-bin.jar.You can use other JDBC-ODBC driver available with JDK but I don’t prefer it. MySQL Connector/J is the official JDBC driver for MySQL. When you download MySQL Connector/J it contains mysql-connector-java-5.1.12-bin.jar. This jar file contains the connection implementations.
  7. Place the jar file into common/lib folder hierarchy of Tomcat Home directory.
  8. To configure Eclipse to use Apache Tomcat as server:
    • Find the Servers view in Eclipse IDE. You can open it using menu Window/Show View/Servers.
    • Right-click on Servers view and select New/Server.
    • Select the server type as Tomcat version installed on your system and click Next.
    • Set the Tomcat installation directory and click finish.
    • Now you will find a “Apache Tomcat at localhost” in the Servers view.
  9. To create Project:
    • Create a new project using menu File/New/Other and then Web/Dynamic Web Project.
    • Enter the name of the project and select the target runtime as Apache Tomcat.
    • Set the configuration as Default configuration for Apache Tomcat and click Finish.
  10. To create Servlet:
    • Go to the folder hierarchy and right-click on the WebContent and select New/Other and then Web/Servlet.
    • Enter Package name and Class name for Servlet and other details and click Finish.
  11. Connection URL:Format of the Connection URL is “jdbc:mysql://[host][:port]/[database][?properties][=values]”.
    For example, “jdbc:mysql:localhost:3306/EXAMPLE?user=root&password=ucancode”.
    Here,

    • host is host name where MySQL Database is running(localhost or 127.0.0.1),
    • port is port number where MySQL Database is running(Default port number for MySQL is 3306),
    • database is database name which is to be connected(Database Name is EXAMPLE), and
    • property is supplied for purposes like username, password, etc(user is “root” and password is “ucancode”).
  12. To run Servlet,
    • Right-click on the Project name in Project Explorer, and select Run as/Run on server.
    • Select the Apache Tomcat and click finish.
    • The output will be generated in browser.

If you have some difficulties or doubts or any troubleshooting, leave a comment.