From Fedora Project Wiki

m (Minor tweaks)
m (Connector is expected to work even on a machine without the server installed)
 
(2 intermediate revisions by one other user not shown)
Line 10: Line 10:
'''Needed:'''
'''Needed:'''
* java-headless >= 1:1.8
* java-headless >= 1:1.8
* mariadb-server


== Example usage ==
== Example usage ==
Line 43: Line 42:
     String url = "jdbc:mariadb://" + host + "/" + dbname;
     String url = "jdbc:mariadb://" + host + "/" + dbname;
     String username = "root";
     String username = "root";
     String password = "password";
     String password = "";
     Connection conn = DriverManager.getConnection(url, username, password);
     Connection conn = DriverManager.getConnection(url, username, password);


Line 67: Line 66:
        
        
       // print the results
       // print the results
      System.out.println("ENGINE, SUPPORT");
       System.out.format("%s, %s, %s, %s, %s, %s\n", engine, support, comment, transactions, xa, savepoints);
       System.out.format("%s, %s, %s, %s, %s, %s\n", engine, support, comment, transactions, xa, savepoints);
     }
     }

Latest revision as of 16:02, 19 February 2020

Article about the mariadb-java-client package

Connector releases

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

Prerequisites

Needed:

  • java-headless >= 1:1.8

Example usage

Install packages:

dnf install mariadb-java-client java-1.8.0-openjdk-devel

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

dnf install mariadb-server
systemctl start mariadb

Create file JavaMysqlSelectExample.java with content:
( Note: the password is empty by default )

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 = "";
    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.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