Class ServerDatabase

java.lang.Object
com.codename1.db.Database
com.codename1.db.ServerDatabase
Direct Known Subclasses:
MySqlDatabase, OracleDatabase, PostgresDatabase, SqlServerDatabase

public class ServerDatabase extends Database

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.

  • 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 example Postgres.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

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

      public void beginTransaction() throws IOException
      Description copied from class: Database

      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
      Specified by:
      beginTransaction in class Database
      Throws:
      IOException
    • commitTransaction

      public void commitTransaction() throws IOException
      Description copied from class: Database

      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
      Specified by:
      commitTransaction in class Database
      Throws:
      IOException
    • rollbackTransaction

      public void rollbackTransaction() throws IOException
      Description copied from class: Database

      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
      Specified by:
      rollbackTransaction in class Database
      Throws:
      IOException
    • close

      public void close()
      Description copied from class: Database

      Closes the database

      Throws
      • IOException
      Specified by:
      close in class Database
    • isClosed

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

      public void execute(String sql) throws IOException
      Description copied from class: Database

      Execute an update query. Used for INSERT, UPDATE, DELETE and similar sql statements.

      Parameters
      • sql: the sql to execute
      Throws
      • IOException
      Specified by:
      execute in class Database
      Throws:
      IOException
    • execute

      public void execute(String sql, String[] params) throws IOException
      Description copied from class: Database

      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: the sql to execute

      • params: to bind to the query where the '?' exists

      Throws
      • IOException
      Specified by:
      execute in class Database
      Throws:
      IOException
    • execute

      public void execute(String sql, Object... params) throws IOException
      Description copied from class: Database

      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: 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:
      execute in class Database
      Throws:
      IOException
    • executeUpdate

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

      public Cursor executeQuery(String sql) throws IOException
      Description copied from class: Database

      This 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:
      executeQuery in class Database
      Throws:
      IOException
    • executeQuery

      public Cursor executeQuery(String sql, String[] params) throws IOException
      Description copied from class: Database

      This 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:
      executeQuery in class Database
      Throws:
      IOException
    • executeQuery

      public Cursor executeQuery(String sql, Object... params) throws IOException
      Description copied from class: Database

      This 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:
      executeQuery in class Database
      Throws:
      IOException