If your Tomcat application needs to read or write data in MySQL, the setup is usually straightforward once you have the correct database credentials, JDBC driver, and connection settings. In a hosted environment, the main goal is to keep the Tomcat service, the database connection, and the application configuration aligned so your app can connect reliably through your control panel and private JVM setup.
In an ITA Java hosting environment with My App Server, you can run your own Apache Tomcat instance inside your hosting account, choose a suitable Java version, and manage the service from Plesk. That makes it practical to connect JSP, servlet, and WAR-based applications to MySQL without needing a full enterprise application platform.
What you need before connecting Tomcat to MySQL
Before you configure the application, make sure you have the following details ready:
- MySQL database name
- MySQL username and password
- MySQL host or server address
- MySQL port, usually 3306
- JDBC driver for MySQL Connector/J
- Tomcat application location, such as a WAR deployment or unpacked web application
- Java version compatible with your application and JDBC driver
If you are using a managed hosting control panel, these values are often shown in the database section, while the My App Server service area is used to manage Tomcat and the Java runtime.
Choose the right connection method
Tomcat applications can connect to MySQL in two common ways:
Direct JDBC connection from the application
This is the simplest option for many JSP and servlet applications. Your application loads the MySQL JDBC driver and opens a connection using a JDBC URL. This works well for smaller and medium-sized hosted applications where you want a direct and predictable setup.
JNDI DataSource in Tomcat
This approach configures the database connection pool inside Tomcat and lets your application request a managed DataSource. It is a good choice if you want cleaner application code and connection pooling handled by the server.
In a Tomcat hosting setup, JNDI is often the preferred method because it separates application logic from database settings and can simplify changes later.
Check MySQL access details in your hosting panel
Start by confirming the database parameters in your hosting control panel. In a typical Plesk-based environment, you should verify:
- the database name
- the assigned user
- the user password
- the correct hostname for MySQL access
- whether remote access is allowed or whether the application must connect locally
For hosted Tomcat applications, the database is often accessed using a local or platform-specific host value. Avoid assuming that localhost is always correct unless your provider clearly documents it for your service.
Install or upload the MySQL JDBC driver
Tomcat needs the MySQL JDBC driver to talk to the database. For modern MySQL servers, use MySQL Connector/J that matches your Java version and application requirements.
Typical options include:
- adding the driver JAR to your application’s
WEB-INF/libdirectory - placing the driver in Tomcat’s shared library path if your hosting setup allows it
- bundling the driver inside the deployed WAR file
For most hosted applications, including the driver in the application package is the most practical choice. It keeps the deployment self-contained and avoids dependency issues if you redeploy later.
Make sure the driver version is compatible with the Java version selected in My App Server. A mismatch between Java, Tomcat, and the JDBC driver can cause startup errors or connection failures.
Example JDBC connection string for MySQL
A standard JDBC URL for MySQL looks like this:
jdbc:mysql://DB_HOST:3306/DB_NAME?useSSL=true&serverTimezone=Europe/London
Replace the placeholders with your real values:
DB_HOST— the MySQL host provided by your hosting companyDB_NAME— your database nameuseSSL=true— use only if your database endpoint expects SSLserverTimezone=Europe/London— useful when the application needs a clear timezone setting
If your hosting setup uses a different SSL policy or requires a specific timezone value, follow the documentation for that service. The exact connection string may vary depending on your MySQL version and Java runtime.
Connect a Tomcat application using JDBC in Java
If you prefer direct JDBC, your Java code usually follows this pattern:
- load the JDBC driver
- open a connection using the database URL
- create a prepared statement
- execute the query
- close resources when finished
In practical terms, the connection section of your code will look similar to this:
Connection conn = DriverManager.getConnection(url, user, password);
Use prepared statements for SQL queries instead of building SQL strings manually. This helps avoid SQL injection and makes the application easier to maintain.
When deploying in a hosted Tomcat environment, keep your database credentials out of source code if possible. Use environment variables, configuration files outside the public web root, or application-specific properties loaded at runtime.
Set up a JNDI DataSource in Tomcat
If your application uses a server-managed database connection, define a JNDI resource in Tomcat and then reference it from the application.
A typical setup includes:
- creating a DataSource resource
- setting the database URL, username, and password
- configuring the driver class
- using the resource name in your application code
Example resource naming often uses a value such as jdbc/MyDB. Your application then looks up that resource through java:comp/env.
This method is especially useful in hosted Tomcat environments where service control and deployment are managed in Plesk, because it keeps configuration centralised and easier to update.
Configure the Tomcat service in My App Server
With My App Server, you can manage your private Tomcat service from the hosting control panel. This is useful when you need to check whether the runtime is running, restart the service after deployment, or select a supported Java version.
Typical service tasks include:
- starting and stopping Tomcat
- restarting after configuration changes
- selecting the application server version
- uploading or updating the WAR file
- checking logs for database errors
If the application starts but cannot connect to MySQL, reviewing the Tomcat logs is often the fastest way to identify the cause. Look for messages about authentication failure, unknown host, SSL mismatch, driver problems, or connection timeout.
Common connection problems and how to fix them
Incorrect database host
If the host value is wrong, your app may show a timeout or “unknown host” error. Confirm the exact hostname in the control panel and use the value intended for application connections.
Wrong username or password
Authentication errors usually mean the database user credentials do not match the database configuration. Recheck the password, and confirm that the user has permission to access the correct database.
Missing JDBC driver
If the driver is not available, Java may throw a “No suitable driver” error. Ensure the MySQL Connector/J JAR is included in the application or available to Tomcat as required by your setup.
Java version mismatch
Some older applications expect older Java syntax or older driver behavior. If your Tomcat app works with one Java version and fails with another, review compatibility between the application, Tomcat, and Connector/J.
SSL or timezone issues
MySQL connection URLs can fail if the server expects SSL settings or timezone parameters that are not included. Adjust the JDBC URL based on the database endpoint requirements.
Connection pool limits
If the app opens too many connections or leaves them open, the database may refuse new ones. Use a properly configured connection pool and always close connections, statements, and result sets.
Best practices for hosted Tomcat and MySQL applications
To keep your application stable in a hosting environment, follow these practical recommendations:
- use prepared statements for all user-driven queries
- store credentials outside public web files
- test the database connection after every deployment
- keep the JDBC driver version aligned with your Java runtime
- monitor logs after Tomcat restarts
- set sensible connection pool limits
- avoid hardcoding host-specific values unless necessary
If you manage multiple deployments, document the database host, database name, and connection settings for each application. This is especially helpful when your hosting account contains more than one Tomcat app or more than one database.
Example deployment workflow in a Plesk-based hosting setup
A practical workflow for connecting Tomcat to MySQL in a managed hosting account is usually:
- Create the MySQL database and user in the control panel.
- Note the exact database host and credentials.
- Upload or deploy the WAR file to the Tomcat application managed by My App Server.
- Add the MySQL JDBC driver if it is not already included.
- Configure the JDBC URL or JNDI DataSource.
- Restart the Tomcat service from the service control area.
- Check the application and Tomcat logs for connection errors.
- Test a simple query or login flow to confirm the connection.
This approach keeps the process clear and makes troubleshooting easier if something changes later.
Security tips for database connections
Database access should be configured carefully, even in shared hosting or private JVM environments. Use these safety measures:
- grant the database user only the permissions the application needs
- use strong passwords
- do not expose database credentials in public source repositories
- prefer parameterised queries
- restrict access to configuration files
- review SSL requirements for the database connection
If your application stores customer data, be especially careful with logging. Avoid writing passwords, tokens, or full SQL queries with sensitive values into the logs.
When to use direct connection versus JNDI
Choose a direct JDBC connection if:
- the application is small
- you want simpler code
- the deployment is self-contained
- you are testing a new Tomcat app quickly
Choose JNDI if:
- you want connection pooling managed by Tomcat
- you need cleaner application code
- you may change database settings later
- you are hosting multiple web applications with similar connection needs
For most hosted Java applications, JNDI offers a good balance of structure and maintainability, while direct JDBC remains useful for simple installations and testing.
FAQ
What JDBC driver should I use for MySQL?
Use MySQL Connector/J that matches your Java version. The driver should be compatible with the Tomcat runtime and the MySQL server version you are using.
Can I connect Tomcat to MySQL on shared hosting?
Yes, if your hosting plan supports Java hosting and database access. In a My App Server environment, Tomcat can run inside your account and connect to MySQL using the database details provided in the control panel.
Why does my application show “Access denied”?
This usually means the database username, password, or permissions are incorrect. Confirm that the user is assigned to the database and has the required privileges.
Do I need to restart Tomcat after changing database settings?
In many cases, yes. If you change JNDI settings, driver placement, or runtime configuration, restart the Tomcat service so the changes take effect.
Should I use localhost for the MySQL host?
Only if your hosting provider documents that value for your setup. Some environments use a different internal hostname for database access.
How do I know whether the problem is in Tomcat or MySQL?
Check the Tomcat logs first. If the application starts but database requests fail, the log usually shows whether the issue is authentication, network access, driver loading, or SQL syntax.
Conclusion
Connecting a Tomcat application to MySQL in a hosted environment is mostly about using the correct database credentials, including the right JDBC driver, and configuring the connection method that fits your application. For many small and medium Java applications, a private Tomcat instance managed through Plesk and My App Server provides a practical way to deploy WAR files, control the Java version, and manage the database connection without extra complexity.
If you keep the configuration tidy, review logs after deployment, and use secure database practices, your Tomcat and MySQL setup should remain stable and easy to maintain.