From Fedora Project Wiki

Revision as of 14:26, 28 February 2018 by Jjanco (talk | contribs) (Create page for mariadb-java-client)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Article about mariadb-java-client package

Connector releases

mariadb-java-client 2.2.2 - current (F28, Rawhide)
mariadb-java-client 2.1.2 - (F27)

Perequisites

Needed:

  • java-headless >= 1:1.8
  • mariadb

Example usage

Install packages:

# yum install mariadb-java-client java-1.8.0-openjdk-devel

If you want to test this example against local database, you also need to have it installed and started.

# yum install mariadb-server
# systemctl start mariadb

Create file JavaMysqlSelectExample.java with content:

import java.sql.*;

public class JavaMysqlSelectExample
{

  public static void main(String[] args) throws Exception
  {
    // Ensure we have mariadb Driver in classpath
    Class.forName("org.mariadb.jdbc.Driver");

    // create our mysql database connection
    String host = "localhost";
    String dbname = "information_schema";
    String url = "jdbc:mariadb://" + host + "/" + dbname;
    String username = "root";
    String password = "password";
    Connection conn = DriverManager.getConnection(url, username, password);

    // our SQL SELECT query. 
    // if you only need a few columns, specify them by name instead of using "*"
    String query = "SELECT * FROM ENGINES";

    // create the java statement
    Statement st = conn.createStatement();
    
    // execute the query, and get a java resultset
    ResultSet rs = st.executeQuery(query);
    
    // iterate through the java resultset
    while (rs.next())
    {
      String engine = rs.getString("ENGINE");
      String support = rs.getString("SUPPORT");
      String comment = rs.getString("COMMENT");
      String transactions = rs.getString("TRANSACTIONS");
      String xa = rs.getString("XA");
      String savepoints = rs.getString("SAVEPOINTS");
      
      // print the results
      System.out.println("ENGINE, SUPPORT");
      System.out.format("%s, %s, %s, %s, %s, %s\n", engine, support, comment, transactions, xa, savepoints);
    }
    st.close();
  }
}

Compile source:

$ javac JavaMysqlSelectExample.java

Run code:

$ java -classpath /usr/lib/java/mariadb-java-client.jar:. JavaMysqlSelectExample

By default java does not include any library from your system. With option -classpath we include library /usr/lib/java/mariadb-java-client.jar, to be able to see MariaDB JDBC driver, also we have to include current directory to see our compiled code.

Example output prints list of available table engines:

ROCKSDB, YES, RocksDB storage engine, YES, YES, YES
MRG_MyISAM, YES, Collection of identical MyISAM tables, NO, NO, NO
CSV, YES, CSV storage engine, NO, NO, NO
BLACKHOLE, YES, /dev/null storage engine (anything you write to it disappears), NO, NO, NO
MyISAM, YES, MyISAM storage engine, NO, NO, NO
ARCHIVE, YES, Archive storage engine, NO, NO, NO
FEDERATED, YES, FederatedX pluggable storage engine, YES, NO, YES
PERFORMANCE_SCHEMA, YES, Performance Schema, NO, NO, NO
InnoDB, DEFAULT, Supports transactions, row-level locking, foreign keys and encryption for tables, YES, YES, YES
Aria, YES, Crash-safe tables with MyISAM heritage, NO, NO, NO
SEQUENCE, YES, Generated tables filled with sequential values, YES, NO, YES
MEMORY, YES, Hash based, stored in memory, useful for temporary tables, NO, NO, NO

Notes

Upstream infromation and documentation