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.

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

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.

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.

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.

MariaDB Auto-Cluster
host=jdbc:mariadb://{hostname}/{db_name}?usePipelineAuth=false
username={user}
password={password}
driver=org.mariadb.jdbc.Driver
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.

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


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=falsefor 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.
