mariadb-connector-j-usage

Guides writing and reviewing Java application code using the MariaDB Connector/J JDBC driver.

28|115|Updated Jan 28, 2025
One-click install
npx skills add https://github.com/mariadb-corporation/mariadb-docs --skill mariadb-connector-j-usage-mariadb-corporation
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: mariadb-connector-j-usage
Source: https://github.com/mariadb-corporation/mariadb-docs/tree/main/agent-skills/granular/connectors/mariadb-connector-j-usage
Command: npx skills add https://github.com/mariadb-corporation/mariadb-docs --skill mariadb-connector-j-usage-mariadb-corporation

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Generated or hand-written Java code against MariaDB often carries assumptions from other JDBC drivers — wrong URL schemes, unnecessary Class.forName calls, MySQL-style fetch-size streaming, or expecting a single generated key from batch inserts — producing bugs, SQL injection risk, or OOM failures. ## Core Features & Use Cases - Driver behavior corrections: Documents that JDBC 4 auto-registers the driver, the jdbc:mariadb:// URL scheme (with permitMysqlScheme for jdbc:mysql:), and that useServerPrepStmts defaults to false so parameter substitution is client-side. - Batch and transaction guidance: Covers addBatch()/executeBatch() with the COM_STMT_BULK protocol, getGeneratedKeys() returning one id per row, and autocommit defaulting to true requiring setAutoCommit(false) for transactions. - Pooling, failover, and error handling: Explains MariaDbPoolDataSource, multi-host failover URLs (sequential:, loadbalance:), sslMode TLS options, and the standard java.sql.SQLException hierarchy. - Use Case: When reviewing a Java service that loops single executeUpdate() calls for bulk inserts, apply this Skill to rewrite it with batched prepared statements and correctly read all generated keys. ## Quick Start Ask the AI to write or review Java code that connects to MariaDB using the mariadb-java-client JDBC driver, such as a pooled batch insert with generated keys.

Frequently Asked Questions about mariadb-connector-j-usage

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I connect to MariaDB from Java with JDBC?▼

Use DriverManager.getConnection with a jdbc:mariadb://host:port/db URL; the driver auto-registers via JDBC 4, so Class.forName is unnecessary. The jdbc:mysql: prefix only works when the permitMysqlScheme option is present in the URL.

How do I batch insert and get generated keys in MariaDB Connector/J?▼

Prepare the INSERT with Statement.RETURN_GENERATED_KEYS, call addBatch() per row, then executeBatch(). Connector/J 3.x sends insert batches via the COM_STMT_BULK protocol by default, and getGeneratedKeys() returns one id per inserted row, not a single id.

Does MariaDB Connector/J use server-side prepared statements by default?▼

No, useServerPrepStmts defaults to false, so PreparedStatement substitutes parameters client-side over the text protocol. Enable it on the URL when reusing a bounded set of statements that fits the prepare cache (prepStmtCacheSize, default 250).

Why does setFetchSize(Integer.MIN_VALUE) throw in MariaDB Connector/J?▼

MariaDB Connector/J requires a fetch size of zero or greater, so Integer.MIN_VALUE throws SQLException. Use a positive value like setFetchSize(1000) on a forward-only statement, and note the server still sends all rows to the client socket.

How do I configure connection pooling with MariaDB Connector/J?▼

Use MariaDbPoolDataSource (maxPoolSize default 8, maxIdleTime default 600s) or wrap the driver URL with an external pool like HikariCP. Calling close() on a pooled connection returns it to the pool and resets autocommit, isolation level, and any active transaction.

How do I set up failover across multiple MariaDB hosts in JDBC?▼

Use multi-host URLs instead of custom retry code: jdbc:mariadb:sequential://host1,host2,host3/db, or the replication:, loadbalance:, and load-balance-read: prefixes. Comma-separate the hosts in a single URL and the driver handles failover natively.