Java Connection

Java Connection to MariaDB/MySQL #

MariaDB and MySQL are highly popular open source databases, used by developers all over the world. In this instruction we’ll show you how to connect your Java application to these databases, standalone server and clustered solution.

1. Log into your CirrusGrid account and create an environment with the MariaDB (or MySQL) database server (available within the SQL wizard section):

  • for standalone database servernew Java environment with standalone database

  • for Auto-Clustering solutionnew Java environment with database auto-cluster

We’ve also added Tomcat node to provide an example of database connection from application server.

2. Check your email inbox – it should contain a message with administration details for the created MariaDB (or MySQL) server.

In case of database cluster, the Entry Point for Connecting is referred to a ProxySQL load balancer.

3. Switch back to the dashboard and click the Open in Browser button for your MariaDB/MySQL node.

MariaDB open in browser

If you have clustered solution, press on Open in Browser next to the master database node (marked as M).

MariaDB auto-cluster open in browser

Log into the opened admin panel using credentials from the above-mentioned emails.

4. Use an existing database (e.g. test) or Create a new one.

MariaDB phpMyAdmin admin panel

5. Return to dashboard and click the Config button next to the application server (Tomcat, in our case) to access configuration file manager.

application server config button

6. Navigate to the /opt/tomcat/temp folder, create a new mydb.cfg file.

For standalone database connection, add the following data in the mydb.cfg file:


All the required info can be found within the MariaDB/MySQL node email:

  • {host} – link to your DB node without protocol part
  • {db_name} – name of the database (test in our case)
  • {user} and {password} – database admin credentials (for production usage, it’s recommended to create a dedicated account with the appropriate permissions)

MySQL connection details

For connection to the cluster, ProxySQL load balancer is used as the entry point, and each type of databases has its own connector. So add the following data to the mydb.cfg file:

For MariaDB:

  • {hostname} – link to your DB cluster load balancer (i.e. ProxySQL layer)
  • {db_name} – name of the database. We chose test in the first step
  • usePipelineAuth – if activated different queries are executed using pipeline (all queries are sent, only then all results are read), permitting faster connection creation. This value should be set to false, as such implementation doesn’t work with the ProxySQL in front of the cluster
  • {user} and {password} – database credentials received in the email

MariaDB auto-cluster connection details

For MySQL:

  • {hostname} – link to your DB cluster load balancer (i.e. ProxySQL layer)
  • {db_name} – name of the database (test in our case)
  • {user} and {password} – database admin credentials (for production usage, it’s recommended to create a dedicated account with the appropriate permissions)

In such a way, all connection settings are saved in a single file, which, subsequently, will be read by the application.

7. For deployment and further connection, we are going to use the following sample application:

package connection;  

import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.SQLException;  
import java.sql.Statement;  
import java.text.SimpleDateFormat;  
import java.util.Date;  
import java.util.Properties;  
import java.util.logging.Level;  
import java.util.logging.Logger;  

public class DbManager {

    public String date = new SimpleDateFormat("dd-MM-yyyy-HH-mm").format(new Date());  
    private final String createTable = "CREATE TABLE `" + date + "` (id INT, data VARCHAR(100));";  
    private static final int LoginTimeout = 10;  

    public DbManager() {  

    public Connection createConnection() throws IOException, ClassNotFoundException, SQLException {  
        Properties prop = new Properties();  
        System.out.println("\n\n=======================\nJDBC Connector Test " + date);  
        System.out.println("User home directory: " + System.getProperty("user.home"));  
        String host;  
        String username;  
        String password;  
        String driver;  
        try {  
            prop.load(new"user.home") + "/mydb.cfg"));  

            host = prop.getProperty("host").toString();  
            username = prop.getProperty("username").toString();  
            password = prop.getProperty("password").toString();  
            driver = prop.getProperty("driver").toString();  
        } catch (IOException e) {  
            System.out.println("Unable to find mydb.cfg in " + System.getProperty("user.home") + "\n Please make sure that configuration file created in this folder.");  
            host = "Unknown HOST";  
            username = "Unknown USER";  
            password = "Unknown PASSWORD";  
            driver = "Unknown DRIVER";  

        System.out.println("host: " + host + "\nusername: " + username + "\npassword: " + password + "\ndriver: " + driver);  

        System.out.println("DRIVER: " + driver);  
        System.out.println("Set Login Timeout: " + LoginTimeout);  
        Connection connection = DriverManager.getConnection(host, username, password);  
        System.out.println("CONNECTION: " + connection);  

        return connection;  

    public String runSqlStatement() {  
        String result = "";  
        try {  
            Statement statement = createConnection().createStatement();  
            System.out.println("SQL query: " + createTable);  
        } catch (IOException | ClassNotFoundException ex) {  
            Logger.getLogger(DbManager.class.getName()).log(Level.SEVERE, null, ex);  
            System.out.println("Exception occurred: " + ex);  
            result = ex.getMessage();  
        } catch (SQLException ex) {  
            result = ex.getMessage();  
        return result;  

 Deploy our example application to your Tomcat server using the following link:

Java deploy from URL

Java application deployment dialog


  • Our example application already contains the jdbc-connectors for MariaDB/MySQL database access. However, to connect your own project, you need to manually upload them to the webapps/{app_context}/WEB-INF/lib folder on your application server.
  • Don’t forget to restart your application server to apply mydb.cfg changes, by pressing Restart Node button.
restart application server nodes

9. Once deployment is finished, click Open in Browser in popup window or next to your application server.

successful deployment dashboard notification

10. In the opened browser tab, click on the Create test table in your database button.

example application of the server connection to database

11. Now, in order to ensure everything works fine, return to the phpMyAdmin panel and navigate to the test database.

new table added to database

You’ll see that the newly created table appeared with the name {date-time of creation}, that means the DB has been successfully accessed and modified from your Java application. It’s that easy!

Useful to Know #

CirrusGrid PaaS also lets you extend your MariaDB/MySQL servers functionality by following the appropriate instructions in our documentation:

  • set up the preferred database replication type with embedded Auto-Clustering feature in order to get the increased DB performance and data loss protection
  • adjust Backups Scheduling for ensuring safety of the information inside your DBs in the case of unexpected server failure
  • see the Remote Access instruction and learn how to access your database remotely via the preferred desktop MySQL client
  • use the Dump Files Import/Export guide to find out how to manually backup and restore your data from the previously created dumps
  • see the Database connection strings to get more info how to configure connection to different db types

Video Tutorial on MySQL Connection to Java Application #

You can use the video tutorial version to see how to create a new environment, configure a MySQL database server, connect to it from your application, and finally, deploy the application to the CirrusGrid PaaS.

Powered by BetterDocs