Class ServerDatabase
- Direct Known Subclasses:
MySqlDatabase, 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.
-
Field Summary
Fields inherited from class Database
COLUMN_BLOB, COLUMN_FLOAT, COLUMN_INTEGER, COLUMN_NULL, COLUMN_TEXT, COLUMN_UNKNOWN, inTransaction -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedServerDatabase(ServerSession.Connector connector, String host, int port, String database, String user, String password, String sslMode, int timeoutMillis) Connects and signs in. -
Method Summary
Modifier and TypeMethodDescriptionvoidStarts a transaction.voidclose()Closes the databasevoidCommits current transactionvoidExecute an update query.voidExecute an update query with params.voidExecute an update query with params.executeQuery(String sql) This method should be called with SELECT type statements that return row set.executeQuery(String sql, Object... params) This method should be called with SELECT type statements that return row set it accepts object with params.executeQuery(String sql, String[] params) This method should be called with SELECT type statements that return row set.intexecuteUpdate(String sql, Object... params) Runs a statement and returns the number of rows it changed.The SQL dialect of the server this session is connected to.booleanisClosed()True once the session is closed or its connection was lost.voidRolls back current transactionMethods inherited from class Database
abandonFailedCommit, beforeFirst, beginTransactionMode, changeKey, checkBeginTransaction, checkEndTransaction, checkNoTransactionForKeyChange, coerceToText, count, decrypt, delete, encrypt, exists, forgetManagedKey, getDatabasePath, hasAttachments, isBlobQueryParameterSupported, isCustomPathSupported, isDatabaseBeingDeleted, isEncrypted, isEncryptionSupported, isInTransaction, isLegacyBehavior, markTransactionEnded, normalizeDatabaseKey, normalizeDatabasePathKey, noteConnectionClosed, noteEngineTransactionState, noteFirstStatementTransactionControl, noteScriptTransactionControl, openDatabaseCount, openOrCreate, openOrCreate, registerOpenDatabase, releaseKeyChangeClaim, releaseOpenDatabase, requireAttachmentsHeld, requireQueryStatement, requireSoleConnectionForKeyChange, reserveAttachments, reserveAttachments, setLegacyBehavior, supportsNestedTransactions, supportsWasNull, toPragmaLiteral, transactionControlKeyword, wasNull
-
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: the engine, for examplePostgres.CONNECTOR -
sslMode:disable; anything else is refused -
timeoutMillis: how long connecting may take, and each wait for the server after it (signing in, a query's reply); 0 for no limit
- Throws:
IOException
-
-
-
Method Details
-
getDialect
The SQL dialect of the server this session is connected to. -
beginTransaction
Description copied from class:DatabaseStarts 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
- Specified by:
beginTransactionin classDatabase- Throws:
IOException
-
commitTransaction
Description copied from class:DatabaseCommits 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
- Specified by:
commitTransactionin classDatabase- Throws:
IOException
-
rollbackTransaction
Description copied from class:DatabaseRolls 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
- Specified by:
rollbackTransactionin classDatabase- Throws:
IOException
-
close
-
isClosed
public boolean isClosed()True once the session is closed or its connection was lost. -
execute
Description copied from class:DatabaseExecute an update query. Used for INSERT, UPDATE, DELETE and similar sql statements.
Parameters
sql: the sql to execute
Throws
IOException
- Specified by:
executein classDatabase- Throws:
IOException
-
execute
Description copied from class:DatabaseExecute 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: the sql to execute -
params: to bind to the query where the '?' exists
Throws
IOException
- Specified by:
executein classDatabase- Throws:
IOException
-
-
execute
Description copied from class:DatabaseExecute 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: the sql to execute -
params: @param params to bind to the query where the '?' exists, supported object types are String, byte[], Double, Long and null
Throws
IOException
- Overrides:
executein classDatabase- Throws:
IOException
-
-
executeUpdate
Runs a statement and returns the number of rows it changed.- Throws:
IOException
-
executeQuery
Description copied from class:DatabaseThis method should be called with SELECT type statements that return row set.
Parameters
sql: the sql to execute
Returns
a cursor to iterate over the results
Throws
IOException
- Specified by:
executeQueryin classDatabase- Throws:
IOException
-
executeQuery
Description copied from class:DatabaseThis method should be called with SELECT type statements that return row set.
Parameters
-
sql: the sql to execute -
params: to bind to the query where the '?' exists
Returns
a cursor to iterate over the results
Throws
IOException
- Specified by:
executeQueryin classDatabase- Throws:
IOException
-
-
executeQuery
Description copied from class:DatabaseThis method should be called with SELECT type statements that return row set it accepts object with params.
Parameters
-
sql: the sql to execute -
params: @param params 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
Throws
IOException
- Overrides:
executeQueryin classDatabase- Throws:
IOException
-
-