MariaDB Client Library for Java Applications
The MariaDB Client Library for Java Applications is used to connect applications developed in Java to MariaDB and MySQL databases using the standard JDBC API. The client library is LGPL licensed.
Introduction
The MariaDB Client Library for Java Applications is a Type 4 JDBC driver. It was developed specifically as a lightweight JDBC connector for use with MySQL and MariaDB database servers. It's originally based on the Drizzle JDBC code, and with a lot of additions and bug fixes.
Obtaining the driver
The driver is downloaded from http://downloads.mariadb.org
Installing the driver
Installation is as simple as placing the .jar file in your classpath.
Requirements
- Java 6
- A MariaDB or MySQL Server
- maven (only if you want build from source)
Source code
The source code is available on Launchpad: https://launchpad.net/mariadb-java-client. Development version can be obtained using
bzr branch lp:mariadb-java-client
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.
Testing the driver
The section deals with building the connector from source and testing it. If you have downloaded a ready built connector, in a jar file, then this section may be skipped.
MariaDB Client Library for Java Applications uses maven. You first need to ensure you have both java and maven installed on your server before you can build the driver.
Currently, the test suite is more development oriented, but we provide information on how to run it in case the user wishes to do so.
Note you will need a MariaDB or MySQL server running on localhost and a schema called test to be able to package the JDBC jar binary (tests need to be run successfully to build the package)
Note: You will also need a MariaDB or MySQL server running on localhost with a no-password root user
To execute the test suite: To run the test suite, you need a running version of MySQL/MariaDB server on localhost with a database called test:
$ mvn test
To compile the jar and run the test suite, use
$ mvn package
Installing the driver
Installation of the client library is very simple, the jar file should be saved in an appropriate place for your application and the classpath of your application altered to include the MariaDB Client Library for Java Applications rather than your current connector.
Using the driver
The following subsections show the formatting of JDBC connection strings for MariaDB, MySQL database servers. Additionally, sample code is provided that demonstrates how to connect to one of these servers and create a table.
As this is a standard JDBC driver, it should be a drop-in replacement for any other driver. Certain features outside the standard from other drivers may not currently be implemented / available.
Driver Manager
Applications designed to use the driver manager to locate the entry point need no further configuration, the MariaDB Client Library for Java Applications will automatically be loaded and used in the way any previous MySQL driver would have been.
Driver Class
The driver class provided by the MariaDB Client Library for Java Applications is org.mariadb.jdbc.Driver.
Connection strings
Format of the JDBC connection string is
jdbc:mysql:<host>:<port>/<database>?<key1>=<value1>&<key2>=<value2>...
Optional URL parameters
General remark:
Following options are currently supported.
key | description |
---|---|
user | Database user name |
password | Password of database user |
fastConnect | If set, skips check for sql_mode, assumes NO_BACKSLASH_ESCAPES is *not* set |
useFractionalSeconds | Correctly handle subsecond precision in timestamps (feature available with MariaDB 5.3 and later).May confuse 3rd party components (Hibernated) |
allowMultiQueries | Allows multiple statements in single executeQuery |
useCompression | allow compression in MySQL Protocol |
useSSL | Force SSL on connection (client certificates are not yet supported) |
tcpNoDelay | Sets corresponding option on the connection socket |
tcpKeepAlive | Sets corresponding option on the connection socket |
tcpRcvBuf | set buffer size for TCP buffer (SO_RCVBUF) |
tcpSndBuf | set buffer size for TCP buffer (SO_SNDBUF) |
tinyInt1isBit | Datatype mapping flag, handle MySQL Tiny as BIT |
yearIsDateType | Year is date type, rather than numerical |
JDBC API Implementation Notes
Streaming result sets
By default, Statement.executeQuery()
will read full result set from server before returning. With large result sets, this will require large amounts of memory. Better behavior in this case would be reading row-by-row, with ResultSet.next()
, so called "streaming" feature. It is activated using ResultSet.setFetchSize(Integer.MIN_VALUE)
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
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "username", "password"); Statement stmt = connection.createStatement(); stmt.executeUpdate("CREATE TABLE a (id int not null primary key, value varchar(20))"); stmt.close(); connection.close();
-
About MariaDB Connector/J
LGPL-licensed MariaDB client library for Java applications. -
Installing MariaDB Connector/J
Various ways to install MariaDB Connector/J. -
Java Connector Using Gradle
Getting started with the Java Connector using Gradle. -
Java Connector Using Maven
Getting started with the Java Connector using Maven. -
Failover and High availability with MariaDB Connector/J
Connecting to another server on failure and allowing load to be distributed over multiple servers -
Option batchMultiSend Description
Description of the Connector/J batchMultiSend option -
Using TLS/SSL with MariaDB Connector/J
How to configure the MariaDB Java driver to support TLS/SSL -
Pool Datasource Implementation
MariaDB has 2 different Datasource implementations. -
GSSAPI Authentication with MariaDB Connector/J
MariaDB has supported GSSAPI authentication since MariaDB 10.1 when the gss... -
List of MariaDB Connector/J Releases
A list of all Connector/J releases -
MariaDB Connector/J Release Notes
Release notes for MariaDB Connector/Java -
MariaDB Connector/J Changelogs
Changelogs for MariaDB Connector/Java -
Failover and High availability with MariaDB Connector/J for 2.x driver
This guide will cover: The load balancing and high availability concepts i...