About MariaDB Connector/J Guide
Complete MariaDB Connector/J guide for Java. Complete JDBC driver reference for connections, prepared statements, and transactions for production use.
What is MariaDB Connector/J?
MariaDB Connector/J is used to connect applications developed in Java to MariaDB and MySQL databases using the standard JDBC API. The library is LGPL licensed.
MariaDB Connector/J is a Type 4 JDBC driver. It was developed specifically as a lightweight JDBC connector for use with MariaDB and MySQL database servers. It was originally based on the Drizzle JDBC code with numerous additions and bug fixes.
Server Compatibility
MariaDB Connector/J is compatible with all MariaDB and MySQL server versions.
Java Compatibility
To determine which MariaDB Connector/J release series would be best to use for each Java version, please see the following table:
Java 25, Java 21, Java 17
MariaDB Connector/J 4.0
JDBC 4.5
Java 25, Java 21, Java 17, Java 11, Java 8
MariaDB Connector/J 3.5, 3.4, 3.3[1]
JDBC 4.5
see parsec authentication restriction
MariaDB Connector/J 4.0 requires Java 17 or later. Applications that run on Java 8 or Java 11 must use the 3.x release series.
Installing MariaDB Connector/J
MariaDB Connector/J can be installed using Maven, Gradle, or by manually putting the .jar file in your CLASSPATH. See Installing MariaDB Connector/J for more information.
MariaDB Connector/J .jar files and source code tarballs can be downloaded from the following URL:
Installing Dependencies
MariaDB Connector/J 4.0 has no required dependency: Unix domain sockets and Windows named pipes use the JDK implementation.
With MariaDB Connector/J 3.x, JNA (net.java.dev.jna:jna) and JNA-PLATFORM (net.java.dev.jna:jna-platform) 4.2.1 or greater are also needed when you would like to connect to the server with Unix sockets or windows pipes.
Using the Driver
The following subsections show the formatting of JDBC connection strings for MariaDB and MySQL database servers. Additionally, sample code is provided that demonstrates how to connect to one of these servers and create a table.
Getting a New Connection
There are two standard ways to get a connection:
Using DriverManager
The preferred way to get a connection with MariaDB Connector/J is to use the DriverManager class.
When the DriverManager class is used to locate and load MariaDB Connector/J, the application needs no further configuration. The DriverManager class will automatically load MariaDB Connector/J and allow it to be used in the same way as any other JDBC driver.
For example:
jdbc:mysql scheme compatibility
MariaDB Connector/J 3.0 only accepts jdbc:mariadb: as the protocol in connection strings by default. When both MariaDB Connector/J and the MySQL drivers are found in the class-path, using jdbc:mariadb: as the protocol helps to ensure that Java chooses MariaDB Connector/J.
Connector/J still allows jdbc:mysql: as the protocol in connection strings when the permitMysqlScheme option is set. For example:
jdbc:mysql://HOST/DATABASE?permitMysqlScheme
(2.x version did permit connection URLs beginning with both jdbc:mariadb and jdbc:mysql)
Using a Pool
Another way to get a connection with MariaDB Connector/J is to use a connection pool.
MariaDB Connector/J provides 2 different Datasource pool implementations:
MariaDbDataSource: The basic implementation. It creates a new connection each time thegetConnection()method is called.MariaDbPoolDataSource: A connection pool implementation. It maintains a pool of connections, and when a new connection is requested, one is borrowed from the pool.
Internal Pool
The driver's internal pool configuration provides a very fast pool implementation and deals with the issues most of the java pool have:
2 different connection states cleaning after release
deals with non-activity (connections in the pool will be released if not used after some time, avoiding the issue created when the server closes the connection after @wait_timeout is reached).
See the pool documentation for more information.
External pool
When using an external connection pool, the MariaDB Driver class org.mariadb.jdbc.Driver must be configured.
Example using hikariCP JDBC connection pool :
Please note that the driver class provided by MariaDB Connector/J is not com.mysql.jdbc.Driver but org.mariadb.jdbc.Driver!
The org.mariadb.jdbc.MariaDbDataSource class can be used when the pool datasource configuration only permits the java.sql.Datasource implementation.
Connection Strings
The format of the JDBC connection string is:
HostDescription:
Some notes about this:
The host must be a DNS name or IP address.
If the host is an IPv6 address, then it must be inside square brackets.
The default port is
3306.The default type is
master.If the failover and load-balancing mode is set to
replication, then the connector assumes that the first host is master, and the others are replicas by default, if their types are not explicitly mentioned.aurora failover prefix is available on 2.x version.
A detailed host description option supersedes a global option description
sslMode, pipe and localSocket are available since 3.4.1 version
the
allowsslMode value is available since 3.5.11 and 4.0.0Before 4.0.0, when a host name resolves to several addresses, the connector tries only one of them: the first address the JVM returns, which is an IPv4 address by default, or an IPv6 address when the JVM runs with
-Djava.net.preferIPv6Addresses=true. If that address is unreachable, for example because IPv4 traffic is blocked, the connection to that host fails without trying the other addresses. Since 4.0.0, the connector tries each resolved address in turn, starting with the address that last connected, within the connectTimeout budget.
Examples:
localhost:3306[2001:0660:7401:0200:0000:0000:0edf:bdd7]:3306somehost.com:3306address=(host=localhost)(port=3306)(type=master)
The jdbc:mariadb:sequential:address=(localSocket=/socket)(sslMode=disable),10.0.0.1:3306/DB?sslMode=verify-full connection string will permit to connect to local unix socket if available, or to host 10.0.0.1 using SSL if not.
Failover and Load-Balancing Modes
Failover and Load-Balancing Modes were introduced in MariaDB Connector/J 1.2.0.
sequential
Description: This mode supports connection failover in a multi-master environment, such as MariaDB Galera Cluster. This mode does not support load-balancing reads on replicas. The connector will try to connect to hosts in the order in which they were declared in the connection URL, so the first available host is used for all queries.
For example, let's say that the connection URL is the following: jdbc:mariadb:sequential:host1,host2,host3/testdbWhen the connector tries to connect, it will always try host1 first. If that host is not available, then it will try host2. etc. When a host fails, the connector will try to reconnect to hosts in the same order.
Introduced: 1.3.0
loadbalance
Description: This mode supports connection load-balancing in a multi-master environment, such as MariaDB Galera Cluster. This mode does not support load-balancing reads on replicas. The connector performs load-balancing for all queries by randomly picking a host from the connection URL for each connection, so queries will be load-balanced as a result of the connections getting randomly distributed across all hosts. Before 2.4.2, this option was named
failover- alias still exist for compatibility -Introduced: 1.2.0
replication
Description: This mode supports connection failover in a primary-replica environment, such as a MariaDB Replication cluster. The mode supports environments with one or more masters. This mode does support load-balancing reads on replicas if the connection is set to read-only before executing the read. The connector performs load-balancing by randomly picking a replica from the connection URL to execute read queries for a connection
Introduced: 1.2.0
load-balance-read
Description: When running a multi-master cluster (i.e. Galera), writing to more than one node can lead to optimistic locking errors ("deadlocks"). Writing concurrently to multiple nodes also doesn't bring a whole lot of performance, due to having to (synchronously) replicate to all nodes anyway. This mode supports connection failover in a multi-master environment, such as MariaDB Galera Cluster. This mode does support load-balancing reads on replicas. The connector will try to connect to primary hosts in the order in which they were declared in the connection URL, so the first available host is used for all queries.
For example, let's say that the connection URL is the following: jdbc:mariadb:load-balance-read:primary1,primary2,address=(host=replica1)(type=replica),address=(host=replica2)(type=replica)/DBWhen the connector tries to connect, it will always try primary1 first. If that host is not available, then it will try primary2. etc. When a primary host fails, the connector will try to reconnect to hosts in the same order.For replica hosts, the connector performs load-balancing for all queries by randomly picking a replica host from the connection URL for each connection, so queries will be load-balanced as a result of the connections getting randomly distributed across all replica hosts.
Introduced: 3.5.1
aurora
Description: This mode supports connection failover in an Amazon Aurora cluster. This mode does support load-balancing reads on replica instances if the connection is set to read-only before executing the read. The connector performs load-balancing by randomly picking a replica instance to execute read queries for a connection
Introduced: 1.2.0 and not supported anymore since 3.0 version
See failover description for more information.
Optional URL Parameters
The connection string options are described in MariaDB Connector/J Connection Parameters.
JDBC API Implementation Notes
Notes on how the driver implements the JDBC API, such as time zone handling, streaming result sets, and LOAD DATA LOCAL INFILE, are in MariaDB Connector/J JDBC API Implementation Notes.
Usage Examples
The following code provides a basic example of how to connect to a MariaDB or MySQL server and create a table.
Creating a Table on a MariaDB or MySQL Server
Services
The driver can be extended with credential, authentication, and SSL factory services. See MariaDB Connector/J Services.
Easy to use logging
In MariaDB Connector/J 3.0, logging can now be enabled at runtime. Connector/J uses the slf4j API if it is installed. Otherwise, Connector/J uses the JDK logger / console.
logger name is "org.mariadb.jdbc".
Connector/J supports the following Java logging levels:
INFO
Logs connection errors
DEBUG/FINE
Logs SQL statements
TRACE/FINEST
Logs network exchanges
Example of configuring "trace" level on driver for logback: file logback.xml in src/main/resources/
Example of generated logs :
Continuous Integration and Automated Tests
For MariaDB Connector/J's continuous integration and automated test results, please see MariaDB Connector/J's Travis CI.
Reporting Bugs
If you find a bug, please report it via the CONJ project on MariaDB's Jira bug tracker.
Source Code
The source code is available at the mariadb-connector-j repository on GitHub.
License
GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
For licensing questions, see the Licensing FAQ.
Upgrading
For the default and behavior changes between release series, see Upgrading MariaDB Connector/J.
F.A.Q.
Error "Could not read resultset: unexpected end of stream, read 0 bytes from 4"
There is an issue communicating with the server.
Most of the time this will be caused by reading a query that has a large resultset; the server usually expects clients to read off the result set relatively quickly. The net_write_timeout server variable controls this behavior (defaults to 60s). If the client doesn't read the whole resultset in that amount of time, the server will discard the connection. If you don't expect results to be handled in this amount of time there is another possibility:
You can use the query "SET STATEMENT net_write_timeout=10000 FOR XXX" with XXX being your "normal" query. This will indicate that specifically for this query, net_write_timeout will be set to a longer time (10000 in this example).
for older servers, a specific query will have to temporarily set net_write_timeout ("SET STATEMENT net_write_timeout=..."), and set it back afterward.
if your application usually uses a lot of long queries with fetch size, the connection can be set using the "sessionVariables=net_write_timeout=xxx" option.
How to Do a Lightweight Ping / Avoid Mass "select 1"
Connection.isValid() is a good approach.
Connection.isValid() is doing a ping (ping in mysql protocol, not network ping).
Connection pool using JDBC4 Validation are using automatically this Connection.isValid()
This page is: Copyright © 2026 MariaDB. All rights reserved.
Last updated
Was this helpful?

