Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class ServerDatabase

  1. Object
  2. Database
  3. ServerDatabase

Known subtypesMySqlDatabase, OracleDatabase, PostgresDatabase, SqlServerDatabase

A Database on a database server rather than a local SQLite file: the base of PostgresDatabase, MySqlDatabase, SqlServerDatabase and OracleDatabase.

Pick the engine by constructing its class. Only the engines an application names are in it; there is no driver lookup and no URL to parse.

String password = Preferences.get("dbPassword", "");
Database db = new PostgresDatabase("db.example.com", 5432, "inventory", "app", password);
try {
    Cursor c = db.executeQuery("SELECT id, name FROM item WHERE price < ?", 10.0);
    while (c.next()) {
        Row r = c.getRow();
        Log.p(r.getLong(0) + " " + r.getString(1));
    }
    c.close();
} finally {
    db.close();
}

Every call waits on the network, so none may run on the EDT: they throw there. Statements use ? for parameters on every engine. Rows are read whole when the query returns, so a cursor can move in both directions.

The connection is plain TCP. This platform’s sockets have no TLS, so sslMode must be disable, written out: the password and every row cross the network unencrypted, so nothing chooses that for the caller. Any other mode, prefer included, is refused rather than quietly downgraded. Reach a server over a network you trust, or through a tunnel.

Constructors

protected ServerDatabase(ServerSession.Connector connector, String host, int port, String database, String user, String password, String sslMode, int timeoutMillis) throws IOExceptionConnects and signs in.

Methods

public Dialect getDialect()The SQL dialect of the server this session is connected to.
public void beginTransaction() throws IOExceptionStarts a transaction.
public void commitTransaction() throws IOExceptionCommits current transaction
public void rollbackTransaction() throws IOExceptionRolls back current transaction
public void close()Closes the database
public boolean isClosed()True once the session is closed or its connection was lost.
public void execute(String sql) throws IOExceptionExecute an update query.
public void execute(String sql, String[] params) throws IOExceptionExecute an update query with params.
public void execute(String sql, Object... params) throws IOExceptionExecute an update query with params.
public int executeUpdate(String sql, Object... params) throws IOExceptionRuns a statement and returns the number of rows it changed.
public Cursor executeQuery(String sql) throws IOExceptionThis method should be called with SELECT type statements that return row set.
public Cursor executeQuery(String sql, String[] params) throws IOExceptionThis method should be called with SELECT type statements that return row set.
public Cursor executeQuery(String sql, Object... params) throws IOExceptionThis method should be called with SELECT type statements that return row set it accepts object with params.

Inherited fields

Inherited methods

Constructor details

ServerDatabase

protected ServerDatabase(ServerSession.Connector connector, String host, int port, String database, String user, String password, String sslMode, int timeoutMillis) throws IOException
Connects and signs in.

Parameters

connector ServerSession.Connector
the engine, for example Postgres.CONNECTOR
host String
Not documented.
port int
Not documented.
database String
Not documented.
user String
Not documented.
password String
Not documented.
sslMode String
disable; anything else is refused
timeoutMillis int
how long connecting may take, and each wait for the server after it (signing in, a query’s reply); 0 for no limit

Method details

getDialect

public Dialect getDialect()
The SQL dialect of the server this session is connected to.

beginTransaction

public void beginTransaction() throws IOException

Starts a transaction.

Transactions are flat. Calling this while a transaction is already open throws, and committing or rolling back returns the connection to autocommit. Closing a database with an open transaction rolls it back.

Throws

IOException
if the database is not open, or a transaction is already in progress

commitTransaction

public void commitTransaction() throws IOException

Commits current transaction

NOTE: Not supported in Javascript port. This method will do nothing when running in Javascript.

Throws

IOException
if database is not opened or transaction was not started

rollbackTransaction

public void rollbackTransaction() throws IOException

Rolls back current transaction

NOTE: Not supported in Javascript port. This method will do nothing when running in Javascript.

Throws

IOException
if database is not opened or transaction was not started

close

public void close()
Closes the database

isClosed

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

execute

public void execute(String sql) throws IOException
Execute an update query. Used for INSERT, UPDATE, DELETE and similar sql statements.

Parameters

sql String
the sql to execute

execute

public void execute(String sql, String[] params) throws IOException
Execute an update query with params. Used for INSERT, UPDATE, DELETE and similar sql statements. The sql can be constructed with ‘?’ and the params will be binded to the query

Parameters

sql String
the sql to execute
params String[]
to bind to the query where the ‘?’ exists

execute

public void execute(String sql, Object... params) throws IOException
Execute an update query with params. Used for INSERT, UPDATE, DELETE and similar sql statements. The sql can be constructed with ‘?’ and the params will be binded to the query

Parameters

sql String
the sql to execute
params Object...
to bind to the query where the ‘?’ exists, supported object types are String, byte[], Double, Long and null

executeUpdate

public int executeUpdate(String sql, Object... params) throws IOException
Runs a statement and returns the number of rows it changed.

executeQuery

public Cursor executeQuery(String sql) throws IOException
This method should be called with SELECT type statements that return row set.

Parameters

sql String
the sql to execute

Returns

a cursor to iterate over the results

executeQuery

public Cursor executeQuery(String sql, String[] params) throws IOException
This method should be called with SELECT type statements that return row set.

Parameters

sql String
the sql to execute
params String[]
to bind to the query where the ‘?’ exists

Returns

a cursor to iterate over the results

executeQuery

public Cursor executeQuery(String sql, Object... params) throws IOException
This method should be called with SELECT type statements that return row set it accepts object with params.

Parameters

sql String
the sql to execute
params Object...
to bind to the query where the ‘?’ exists, supported object types are String, byte[], Double, Long and null

Returns

a cursor to iterate over the results