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.
- Install MySQL Database.
- Install Apache Tomcat.
- Install Eclipse IDE for Java EE Developers.
- 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.
- 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.
- 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. - Place the jar file into common/lib folder hierarchy of Tomcat Home directory.
- 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.
- 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.
- 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.
- 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”).
- 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.


7 comments
Comments feed for this article
November 15, 2010 at 8:06 am
Sukanya
Thank you it helped me
December 3, 2010 at 3:47 pm
bet365
hi I was fortunate to approach your Topics in bing
your Topics is impressive
I get much in your topic really thanks very much
btw the theme of you site is really splendid
where can find it
December 11, 2010 at 2:52 pm
andrenn
Great Job!
February 4, 2011 at 6:35 pm
ucancode
thank you all for appreciating my efforts.
February 14, 2011 at 4:54 pm
MySQL and JDBC « Web Architecture student's project
[...] advices from here and from here chosed “complete installiation” and server configuration: “typical”. [...]
September 28, 2011 at 8:09 pm
darrell
I have had this problem for a while. your explanation works! Thanks!!
December 1, 2011 at 4:13 pm
uchii
i dont understand step number 11. where to put those command?