The Database Object

A Database object represents a JDBC SQL connection to a database.

Once a Database object has been created it can then be used to send SQL queries to the database and retrieve the results.

Prior to Active Web it was only possible to have one cursor open and active per Database object. Active Web does not have this limitation.

The Database object is of class Database.

Database Object Properties

Property

Description

error

An error message If an error has occurred. If no error has occurred then it will be null. The error messages are explained below:

Invalid connection parameters - The constructor does not have enough arguments (minimum is three) to connect to the database.

No Database connected - The beginTransaction, cursor or execute function has been called when the database is not connected.

Attempting to use a connection that is already in use - The beginTransaction, cursor or execute function has been called when a cursor from a previous cursor function is still open.

Invalid statement entered - The execute function has been called with an invalid SQL statement.

No current transaction - The endTransaction function has been called when not in a transaction.

While accessing the database the operating system can generate various other error messages.

prototype

A reference to the Object Prototype. This property does not enumerate and can not be deleted but can be changed.

Database Object Functions

The transaction functions (beginTransaction, endTransaction & rollBack) will only work if the database supports transactions.

Function

Description

beginTransaction()

Start a database transaction.

close()

Close the database connection freeing all resources.

cursor(sql)

Executes the SQL statement in the sql argument and returns a Cursor object using the ResultSet returned by the database. If an SQL error occurs the error text is put into the error property.

endTransaction()

Finish a database transaction.

execute(sql)

Execute the SQL statement in the sql argument. This will not return any results. This is used for updates that do not return any results. If an SQL error occurs the error text is put into the error property.

rollBack()

Roll back a database transaction.

preparedStatement(sql)

Creates a PreparedStatement object using the supplied SQL statement in the sql argument..Should an error occur then the error property of the database object will be set.

An Example of Using the Database Object

Shown below is an example of using the Database object with a JDBC/ODBC bridge.

myDbase = new database(“jdbc:odbc:myODBCSource”,”myUser”,”myPassword”,3);

if (!myDbase.error) {

  // continue

  var myQuery = “select * from myTable where myID =5678;”;

  var myCursor = myDbase.cursor(myQuery);

  if (!myDbase.error) {

      // continue on and read data

  }

  else {

    // error handling code

    writeln(“Error running query: “+myDbase.error);

  }

}

else {

  // error handling code

  writeln(“Error connecting to database: “+myDbase.error);  

}


The Database Constructor

Using the new Operator with the Database Constructor

The Database constructor is used to create new Database objects. The constructor can have a single argument.

var myDatabase = new Database(connectionString, user, password, [poolSize]);

The connectionString identifies the JDBC driver (see adding jdbc drivers) that is to be used and is of the form "jdbc:subprotocol:subname".

The user is a user name within the database environment. The password is the password for that user.

The poolSize defines the number of simultaneous connections allowed for this set of Database parameters. If the poolSize is specified then a pool of connections to the Database is built. These connections remain after the Database has done a close. If the simultaneous connection request exceeds the poolSize then connections will wait in a queue for the next free connection. The poolSize can be changed at any time. If the poolSize is not specified then an immediate one off connection is made that disconnects on a close. Pooled connections use up resources keeping the connection open but improve performance . There is a separate pool for every different set of Database parameters.

Shown below is the Database object prototype chain.

Calling the Database Constructor as a Function

When the constructor is run as a function without the new operator it has exactly the same behaviour as using the new operator.

Database Constructor Properties

Constructor properties are read only (they cannot be changed or deleted) and it is not possible to add new properties to the Constructor.

Property

Description

prototype

A reference to the Object Prototype. This property does not enumerate and cannot be changed or deleted.

Database Constructor Functions

The Database constructor has no predefined functions.

Comment on this topic

Topic ID: 150045