Java Connection

Java Connection to MariaDB, MySQL and Percona

This guide shows how to connect a Java application to standalone or clustered MariaDB, MySQL, or Percona databases. The source example uses Tomcat and stores the JDBC connection settings in a single mydb.cfg file.

Create the Environment

Create a Java environment with MariaDB or MySQL from the SQL section of the topology wizard. Add a Tomcat node to demonstrate the application-to-database connection.

Java environment with standalone database
Java environment with a standalone database server.

For a highly available database, enable Auto-Clustering instead of using a standalone database node.

Java environment with database auto-cluster
Java environment with a database Auto-Cluster.

Prepare the Database

Check the email sent by the platform for the database administration URL and credentials. For a database cluster, the connection entry point is the ProxySQL load balancer.

Open MariaDB in browser
Open the database administration panel.

For a clustered solution, open the master database node marked with M, sign in, and use an existing database such as test or create a new one.

MariaDB phpMyAdmin panel
Select or create the target database.

Create mydb.cfg

Open Config beside Tomcat, navigate to /opt/tomcat/temp, and create mydb.cfg.

Standalone Database

host=jdbc:mysql://{host}/{db_name}
username={user}
password={password}
driver=com.mysql.jdbc.Driver

Use the database host without the protocol prefix, the target database name, and the credentials supplied in the database email. For production, create a dedicated database account with only the required permissions.

MySQL connection details
Use the database connection details from the platform email.

MariaDB Auto-Cluster

host=jdbc:mariadb://{hostname}/{db_name}?usePipelineAuth=false
username={user}
password={password}
driver=org.mariadb.jdbc.Driver
ProxySQL: use the cluster load-balancer hostname. Keep usePipelineAuth=false, because pipeline authentication is not compatible with ProxySQL in front of the cluster.

MySQL Auto-Cluster

host=jdbc:mysql://{host}/{db_name}
username={user}
password={password}
driver=com.mysql.jdbc.Driver

Deploy and Test the Java Application

The sample Java application reads mydb.cfg, loads the JDBC driver, creates a database connection with DriverManager, and executes a CREATE TABLE statement whose table name contains the current date and time.

Properties prop = new Properties();
prop.load(new FileInputStream(
    System.getProperty("user.home") + "/mydb.cfg"
));

String host = prop.getProperty("host");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
String driver = prop.getProperty("driver");

Class.forName(driver);
DriverManager.setLoginTimeout(10);
Connection connection =
    DriverManager.getConnection(host, username, password);

Deploy the sample application to Tomcat through Deployment Manager. The source sample already includes MariaDB and MySQL JDBC connectors. For your own application, upload the required connector to webapps/{app_context}/WEB-INF/lib.

Java deploy from URL
Deploy the sample Java application.

Restart the application-server node after changing mydb.cfg, then open the deployed application and click Create test table in your database.

Restart Tomcat
Restart Tomcat to apply the connection-file changes.
New table added to database
The timestamp-named table confirms successful database access from Java.

Expected Result

The Tomcat application reads the JDBC settings from mydb.cfg, connects to the standalone database or the ProxySQL cluster endpoint, and successfully creates a new test table.

Important Notes

  • Use ProxySQL as the connection entry point for a database cluster.
  • Use a dedicated database account with appropriate permissions for production.
  • Keep usePipelineAuth=false for clustered MariaDB.
  • Install the appropriate JDBC connector in your own application.
  • Restart Tomcat after updating mydb.cfg.
  • The source example uses Tomcat and a database named test.