Could someone provide a few details on how to configure Tomcat to access MySQL?
In which directory within Tomcat do I place mysql-connector-java-5.1.13-bin
? Should I place it under Tomcat 6.0\webapps\myapp\WEB-INF\lib
?
Do I need to add configuration to context.xml
or server.xml
?
Should I create a web.xml
file and place it under Tomcat 6.0\webapps\myapp\WEB-INF
? If so, then what should the contents of this file look like?
Go ahead and open MySQL Workbench and let's connect to this new local server. Click on the “New Connection” icon and leave everything default, except the “Connection Name,” here enter localhost . Double click on the new connection and enter the password you created during installation. Voila!
Default value is 10. maxWait. (int) The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception. Default value is 30000 (30 seconds)
1: Where to place
mysql-connector-java-5.1.13-bin
in Tomcat directory? Should I place it underTomcat 6.0\webapps\myapp\WEB-INF\lib
?
That depends on where the connections are to be managed. Normally you would like to create a connection pooled JNDI datasource to improve connecting performance. In that case, Tomcat is managing the connections and need to have access to the JDBC driver. You should then drop the JAR file in Tomcat/lib
.
But if you're doing it the basic way using DriverManager#getConnection()
, then it in fact don't matter if you drop it in Tomcat/lib
or YourApp/WEB-INF/lib
. You however need to realize that the one in Tomcat/lib
will apply for all deployed webapps and that the one in YourApp/WEB-INF/lib
will override the one in Tomcat/lib
for only the particular webapp.
2: Do I need to confirgure
context.xml
orserver.xml
files?
That depends on where the connections are to be managed. When using a JNDI datasource, it suffices to configure it using YourApp/META-INF/context.xml
like follows (just create file if not exist):
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/yourdb" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
url="jdbc:mysql://localhost:3306/yourdb"
driverClassName="com.mysql.jdbc.Driver"
username="yourname" password="yourpass"
/>
</Context>
and the YourApp/WEB-INF/web.xml
as follows:
<resource-env-ref>
<resource-env-ref-name>jdbc/yourdb</resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>
If you're doing it the basic DriverManager
way, then it's all up to you. Hardcoded, properties file, XML file, etcetera. You should manage it youself. Tomcat won't (and can't) do anything useful for you.
Noted should be that the YourApp/META-INF/context.xml
is specific to Tomcat and clones. Each servletcontainer/appserver has its own way of defining JNDI resources. In Glassfish for example, you'd like to do that through the webbased admin interface.
3: Should I write
web.xml
file and need to place underTomcat 6.0\webapps\myapp\WEB-INF
? If Yes, then what should be the contents of file?
You should always supply one. It's not only to configure resources, but also to define servlets, filters, listeners and that kind of mandatory stuff to run your webapp. This file is part of the standard Servlet API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With