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.
20 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?
June 17, 2012 at 7:09 pm
prince
I did same thing but its giving me an exception saying (Access denied for user’ ‘@’ ‘localhost’ to database databasename )
June 25, 2012 at 9:24 am
nilesh
I did same thing but its giving me an exception saying (Access denied for user’ ‘@’ ‘localhost’ to database
February 18, 2015 at 11:40 am
Pawan
Bhai open your mysql server first by going to services in control panel and type username and password there.if not configured just hit enter when asked for password.
June 25, 2012 at 9:28 am
nilesh
i did same but have a problem ping failed….for ping & could not connect for connect please give me a solution immediately.
November 10, 2012 at 3:23 pm
B r srinivasa
I have done everything as per this protocol when I execute get a 404 status code error. Please suggest what else needed to rectify error. However, I get ping as successful.
Srinivasa
February 7, 2013 at 4:35 pm
neeraj36
thanks sir …..its very helpful for me
March 15, 2013 at 9:15 am
dinesh
thanks for hepling
March 16, 2013 at 8:03 pm
lingaraj
thank you very much sir….This is very helpful for me….
March 24, 2013 at 5:36 am
Kasey
I’m not generally one regarding writing critiques but thought I would pencil my (very) limited experience with ecigs.
June 18, 2013 at 5:19 am
fresh vending
It’s very easy to find out any matter on web as compared to textbooks, as I found this post at this web site.
August 4, 2013 at 8:53 pm
bobby genovese
Hey there just wanted to give you a quick heads
up. The words in your post seem to be running off the screen in Internet explorer.
I’m not sure if this is a format issue or something to do with web browser compatibility but I figured I’d
post to let you know. The layout look great though!
Hope you get the issue fixed soon. Kudos
April 7, 2014 at 7:52 pm
jatin
(1) unable to follow step 11
(2) while running encountering this error
Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use. The server may already be running in another process, or a system process may be using the port. To start this server you will need to stop the other process or change the port number(s).
February 18, 2015 at 11:37 am
wnfiniwenf
@Jatin this error flashes because may be your ports 8005,8080,8009 is in use with some other services .You can change ports in eclipse itself by double clicking the server listed in the servers pane below.then maximize it (again by double clicking) and on the right hand side you will see ports 8005,8080 & 8009. you can change them to(for example.)8100.8101,8102.it will work fine.