(fix typo) |
m (Connector is expected to work even on a machine without the server installed) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
Article about '''mariadb-java-client''' package | Article about the '''mariadb-java-client''' package | ||
== Connector releases == | == Connector releases == | ||
Line 10: | Line 10: | ||
'''Needed:''' | '''Needed:''' | ||
* java-headless >= 1:1.8 | * java-headless >= 1:1.8 | ||
== Example usage == | == Example usage == | ||
Line 16: | Line 15: | ||
Install packages: | Install packages: | ||
<pre> | <pre> | ||
dnf install mariadb-java-client java-1.8.0-openjdk-devel | |||
</pre> | </pre> | ||
If you want to test this example against local database, you also need to have it installed and started. | If you want to test this example against a local database, you also need to have it installed and started. | ||
<pre> | <pre> | ||
dnf install mariadb-server | |||
systemctl start mariadb | |||
</pre> | </pre> | ||
Create file ''JavaMysqlSelectExample.java'' with content: | Create file ''JavaMysqlSelectExample.java'' with content:<br /> | ||
( Note: the password is empty by default ) | |||
<pre> | <pre> | ||
import java.sql.*; | import java.sql.*; | ||
Line 42: | Line 42: | ||
String url = "jdbc:mariadb://" + host + "/" + dbname; | String url = "jdbc:mariadb://" + host + "/" + dbname; | ||
String username = "root"; | String username = "root"; | ||
String password = " | String password = ""; | ||
Connection conn = DriverManager.getConnection(url, username, password); | Connection conn = DriverManager.getConnection(url, username, password); | ||
Line 66: | Line 66: | ||
// print the results | // print the results | ||
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); | ||
} | } | ||
Line 76: | Line 75: | ||
Compile source: | Compile source: | ||
<pre> | <pre> | ||
javac JavaMysqlSelectExample.java | |||
</pre> | </pre> | ||
Run code: | Run code: | ||
<pre> | <pre> | ||
java -classpath /usr/lib/java/mariadb-java-client.jar:. JavaMysqlSelectExample | |||
</pre> | </pre> | ||
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. | 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. |
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