Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public final class MySql

  1. Object
  2. ServerSession
  3. MySql

A MySQL client speaking the client/server protocol directly, for the same reason as Postgres: a translated server binary has no JDBC.

Everything goes through prepared statements (COM_STMT_PREPARE / EXECUTE) and therefore through MySQL’s BINARY result format. That is more code than sending COM_QUERY text, and it is the only way to bind a parameter – a text-protocol client has to build SQL by concatenation, and this API refuses to offer that. The exception is a statement the server refuses to prepare, which binds nothing and goes as text; see command.

Authentication covers what is actually deployed: caching_sha2_password (the MySQL 8 default) and mysql_native_password (5.7 and MariaDB). The caching_sha2 FULL exchange – which the server demands the first time a password is used, before its cache is warm – sends the password to the server, so this client does it only on a TLS connection and says so rather than falling back to the RSA-wrapped variant, which would be a second cryptographic path to get wrong.

Fields

public static final ServerSession.Connector CONNECTOROpens MySql sessions; naming this constant is what puts the engine in an application.

Methods

public Dialect dialect()The SQL this session’s server speaks.
public boolean ownsTransactionControl()True when beginning, committing and rolling back are protocol requests or session state of this client’s own, rather than statements a caller could equally run.
public boolean isMariaDb()See mariaDb.
public static MySql connect(String host, int port, String database, String user, String password, String sslMode, String caFile, int timeoutMillis, int socketTimeoutMillis) throws IOExceptionConnects, optionally upgrades to TLS, and authenticates.
public int execute(String sql, Object[] params) throws IOExceptionRuns a statement that returns no rows.
public List query(String sql, Object[] params) throws IOExceptionRuns a query: a list of Map rows keyed by column label, in column order.
public void begin() throws IOExceptionOpens a transaction.
public void commit() throws IOExceptionCommits the transaction.
public void rollback() throws IOExceptionRolls the transaction back.
public void beginReadOnly() throws IOExceptionOpens a transaction that refuses writes.
public void begin(boolean readOnly, TransactionIsolation isolation) throws IOExceptionUses a next-transaction setting, never a session default.
public void savepoint(String verb, String name) throws IOExceptionSavepoint control, through the text protocol for the same reason as begin.
public long lastInsertId()The key the last INSERT generated, for an engine that reports one that way; 0 otherwise.
public void close()Closes the session; closing twice does nothing.
public boolean isClosed()True once the session is closed or its connection was lost.

Inherited nested types

Inherited methods

Field details

CONNECTOR

public static final ServerSession.Connector CONNECTOR
Opens MySql sessions; naming this constant is what puts the engine in an application. See ServerSession.Connector.

Method details

dialect

public Dialect dialect()
The SQL this session’s server speaks.

ownsTransactionControl

public boolean ownsTransactionControl()
True when beginning, committing and rolling back are protocol requests or session state of this client’s own, rather than statements a caller could equally run.

isMariaDb

public boolean isMariaDb()
See mariaDb.

connect

public static MySql connect(String host, int port, String database, String user, String password, String sslMode, String caFile, int timeoutMillis, int socketTimeoutMillis) throws IOException

Connects, optionally upgrades to TLS, and authenticates.

sslMode is “require”, “prefer” or “disable”, as in Postgres.

execute

public int execute(String sql, Object[] params) throws IOException
Runs a statement that returns no rows.

Returns

the number of rows it changed

query

public List query(String sql, Object[] params) throws IOException
Runs a query: a list of Map rows keyed by column label, in column order.

begin

public void begin() throws IOException
Opens a transaction. See command for why this is not execute.

commit

public void commit() throws IOException
Commits the transaction.

rollback

public void rollback() throws IOException
Rolls the transaction back.

beginReadOnly

public void beginReadOnly() throws IOException
Opens a transaction that refuses writes.

begin

public void begin(boolean readOnly, TransactionIsolation isolation) throws IOException
Uses a next-transaction setting, never a session default. The enum keeps arbitrary SQL out of the text-protocol transaction control path.

savepoint

public void savepoint(String verb, String name) throws IOException
Savepoint control, through the text protocol for the same reason as begin. The name is checked to be a plain identifier here as well as by the caller, because this is the one text-protocol entry point that takes a value at all.

lastInsertId

public long lastInsertId()
The key the last INSERT generated, for an engine that reports one that way; 0 otherwise.

close

public void close()
Closes the session; closing twice does nothing.

isClosed

public boolean isClosed()
True once the session is closed or its connection was lost.