JDBC 4.2 compatible driver providing comprehensive database connectivity for MariaDB and MySQL servers
—
Core connection functionality for establishing and managing database connections with comprehensive configuration options, SSL/TLS security, and multi-host support.
The main JDBC Driver class for connecting to MariaDB and MySQL databases.
/**
* MariaDB JDBC Driver implementation
*/
public final class Driver implements java.sql.Driver {
/**
* Connect to database using JDBC URL and properties
* @param url JDBC connection URL
* @param props Connection properties
* @return Connection to the database
* @throws SQLException if connection fails
*/
public Connection connect(String url, Properties props) throws SQLException;
/**
* Connect using a Configuration object
* @param configuration Pre-built configuration
* @return Connection to the database
* @throws SQLException if connection fails
*/
public static Connection connect(Configuration configuration) throws SQLException;
/**
* Check if driver accepts the given URL
* @param url JDBC URL to validate
* @return true if URL is acceptable
* @throws SQLException if URL validation fails
*/
public boolean acceptsURL(String url) throws SQLException;
/**
* Get configuration properties information
* @param url JDBC URL
* @param info Current properties
* @return Array of property information
* @throws SQLException if property retrieval fails
*/
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException;
/**
* Quote SQL identifier for safe use in queries
* @param identifier Identifier to quote
* @param alwaysQuote Force quoting even if not needed
* @return Quoted identifier
*/
public static String enquoteIdentifier(String identifier, boolean alwaysQuote);
/**
* Quote SQL string literal for safe use in queries
* @param val String value to quote
* @return Quoted string literal
*/
public static String enquoteLiteral(String val);
/**
* Check if identifier is simple (no special characters)
* @param identifier Identifier to check
* @return true if identifier is simple
*/
public static boolean isSimpleIdentifier(String identifier);
}Usage Examples:
// Basic connection using DriverManager
String url = "jdbc:mariadb://localhost:3306/mydb";
Properties props = new Properties();
props.setProperty("user", "myuser");
props.setProperty("password", "mypass");
Connection conn = DriverManager.getConnection(url, props);
// Connection with SSL
String sslUrl = "jdbc:mariadb://localhost:3306/mydb?sslMode=VERIFY_FULL&serverSslCert=/path/to/cert.pem";
Connection secureConn = DriverManager.getConnection(sslUrl, "user", "pass");
// Quote identifiers and literals safely
String quotedTable = Driver.enquoteIdentifier("my table", true); // "`my table`"
String quotedValue = Driver.enquoteLiteral("O'Brien"); // "'O\\'Brien'"MariaDB's Connection implementation with standard JDBC functionality plus driver-specific extensions.
/**
* MariaDB Connection implementation
*/
public class Connection implements java.sql.Connection {
/**
* Cancel the currently executing query (MariaDB-specific)
* @throws SQLException if cancellation fails
*/
public void cancelCurrentQuery() throws SQLException;
// Standard JDBC Connection methods
public Statement createStatement() throws SQLException;
public PreparedStatement prepareStatement(String sql) throws SQLException;
public CallableStatement prepareCall(String sql) throws SQLException;
public DatabaseMetaData getMetaData() throws SQLException;
public void setAutoCommit(boolean autoCommit) throws SQLException;
public boolean getAutoCommit() throws SQLException;
public void commit() throws SQLException;
public void rollback() throws SQLException;
public void close() throws SQLException;
public boolean isClosed() throws SQLException;
public void setTransactionIsolation(int level) throws SQLException;
public int getTransactionIsolation() throws SQLException;
}Usage Examples:
// Create different statement types
Connection conn = DriverManager.getConnection(url, user, pass);
// Simple statement
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM users");
// Prepared statement with parameters
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM users WHERE age > ? AND city = ?");
pstmt.setInt(1, 25);
pstmt.setString(2, "New York");
ResultSet prs = pstmt.executeQuery();
// Callable statement for stored procedures
CallableStatement cstmt = conn.prepareCall("{call getUserStats(?, ?)}");
cstmt.setInt(1, 100);
cstmt.registerOutParameter(2, Types.INTEGER);
cstmt.execute();
int count = cstmt.getInt(2);
// Transaction management
conn.setAutoCommit(false);
try {
// Execute multiple operations
stmt.executeUpdate("INSERT INTO orders (user_id, amount) VALUES (1, 100.00)");
stmt.executeUpdate("UPDATE users SET balance = balance - 100 WHERE id = 1");
conn.commit();
} catch (SQLException e) {
conn.rollback();
throw e;
}
// Cancel long-running query (MariaDB-specific feature)
// In another thread:
conn.cancelCurrentQuery();Standard JDBC URL format with MariaDB-specific extensions for high availability and advanced configuration.
Basic Format:
jdbc:mariadb://host:port/database?option1=value1&option2=value2High Availability Format:
jdbc:mariadb:replication://primary:3306,replica:3306/database
jdbc:mariadb:loadbalance://host1:3306,host2:3306,host3:3306/databaseAdvanced Host Format:
jdbc:mariadb://address=(host=server1)(port=3306)(type=primary),address=(host=server2)(port=3306)(type=replica)/databaseSpecial Connection Types:
jdbc:mariadb://address=(pipe=\\\\.\\pipe\\mysql)/database // Windows named pipe
jdbc:mariadb://address=(localSocket=/var/run/mysqld/mysqld.sock)/database // Unix socketCommon Configuration Examples:
// SSL connection
String sslUrl = "jdbc:mariadb://localhost:3306/mydb?sslMode=VERIFY_FULL&trustStore=/path/to/truststore.jks";
// Connection pooling
String poolUrl = "jdbc:mariadb://localhost:3306/mydb?pool=true&maxPoolSize=20&minPoolSize=5";
// High availability with failover
String haUrl = "jdbc:mariadb:replication://primary:3306,replica1:3306,replica2:3306/mydb?retriesAllDown=3";
// Performance tuning
String perfUrl = "jdbc:mariadb://localhost:3306/mydb?useCompression=true&cachePrepStmts=true&prepStmtCacheSize=250";
// Authentication
String authUrl = "jdbc:mariadb://localhost:3306/mydb?credentialType=ENV&restrictedAuth=mysql_native_password";Utility class for managing database host addresses and connection endpoints.
/**
* Database host address configuration
*/
public class HostAddress {
/**
* Create host address for standard TCP connection
* @param host Hostname or IP address
* @param port Port number
* @return HostAddress instance
*/
public static HostAddress from(String host, int port);
/**
* Create host address with primary/replica designation
* @param host Hostname or IP address
* @param port Port number
* @param primary true if primary server, false if replica
* @return HostAddress instance
*/
public static HostAddress from(String host, int port, boolean primary);
/**
* Create host address for Windows named pipe
* @param pipe Named pipe path
* @return HostAddress instance for pipe connection
*/
public static HostAddress pipe(String pipe);
/**
* Create host address for Unix domain socket
* @param localSocket Unix socket path
* @return HostAddress instance for socket connection
*/
public static HostAddress localSocket(String localSocket);
/**
* Parse connection string into host addresses
* @param spec Connection specification string
* @param haMode High availability mode
* @return List of parsed host addresses
*/
public static List<HostAddress> parse(String spec, HaMode haMode);
// Connection tracking methods
public Long getThreadConnectedTimeout();
public int getThreadsConnected();
}Usage Examples:
// Create host addresses programmatically
HostAddress primary = HostAddress.from("db-primary", 3306, true);
HostAddress replica1 = HostAddress.from("db-replica-1", 3306, false);
HostAddress replica2 = HostAddress.from("db-replica-2", 3306, false);
// Special connection types
HostAddress pipeAddr = HostAddress.pipe("\\\\.\\pipe\\mysql");
HostAddress socketAddr = HostAddress.localSocket("/var/run/mysqld/mysqld.sock");
// Parse connection string
List<HostAddress> hosts = HostAddress.parse(
"primary:3306,replica1:3306,replica2:3306",
HaMode.REPLICATION
);// Standard connection lifecycle
Connection conn = null;
try {
// Establish connection
conn = DriverManager.getConnection(url, user, password);
// Use connection
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM table");
// Process results
while (rs.next()) {
// Process row
}
} catch (SQLException e) {
// Handle exceptions
e.printStackTrace();
} finally {
// Always close connection
if (conn != null && !conn.isClosed()) {
conn.close();
}
}
// Try-with-resources (recommended)
try (Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM table")) {
while (rs.next()) {
// Process row
}
} catch (SQLException e) {
e.printStackTrace();
}Install with Tessl CLI
npx tessl i tessl/maven-org-mariadb-jdbc--mariadb-java-client