# Class: Database

## helma.Database(source)

This class provides access to a relational database through JDBC. There are two convenient ways to create instances of this class.

The first is to use helma.Database.getInstance() to obtain a connection to a DB that is defined in the application's db.properties and managed by Helma. The second way is to define and create a database connection locally using helma.Database.createInstance() and passing it all necessary parameters.

This class provides two ways of interaction: The #query method allows to issue SQL queries, returning a result set. The #execute provides a way to issue SQL statements that do not return a result set.

Database connections allocated by this class are be managed and eventually disposed by Helma.

## Constructor

#### new Database(source)

Constructor for Database objects, providing access through relational databases through JDBC. It is usually simpler to use one of the factory methods #createInstance or #getInstance.

##### Parameters:

Name | Type | Description
--- | --- | ---
`source` | DbSource | instance of a helma.objectmodel.db.DbSource

Source:
  - helma/Database.js, line 53

### Methods

#### execute(sql) → {int}

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL data definition statement. The return value is an integer that indicates the number of rows that were affected by the statement.

##### Parameters:

Name | Type | Description
--- | --- | ---
`sql` | String | an SQL statement

Source:
  - helma/Database.js, line 193

##### Returns:

either the row count for INSERT, UPDATE or DELETE statements, or 0 for SQL statements that return nothing

Type
  int

#### getConnection() → {java.sql.Connection}

Get the java.sql.Connection for this Database instance. This can be used to operate on the connection directly, without going through the helma.Database class.

Source:
  - helma/Database.js, line 68

##### Returns:

the JDBC connection

Type
  java.sql.Connection

#### getDriverName() → {String}

Return the name of the JDBC driver used by this Database instance.

Source:
  - helma/Database.js, line 228

##### Returns:

the JDBC driver name

Type
  String

#### getName() → {String}

Return the name of the Helma DbSource object.

Source:
  - helma/Database.js, line 220

##### Returns:

the DbSource name

Type
  String

#### getProductName() → {String}

Returns the lower case name of the underlying database product.

Source:
  - helma/Database.js, line 76

##### Returns:

the name of the DB product

Type
  String

#### isMySql() → {boolean}

Returns true if this is a MySQL database.

Source:
  - helma/Database.js, line 92

##### Returns:

true if this is an MySQL database.

Type
  boolean

#### isOracle() → {boolean}

Returns true if this is an Oracle database.

Source:
  - helma/Database.js, line 84

##### Returns:

true if this is an Oracle database.

Type
  boolean

#### isPostgreSql() → {boolean}

Returns true if this is a PostgreSQL database.

Source:
  - helma/Database.js, line 100

##### Returns:

true if this is a PostgreSQL database.

Type
  boolean

#### query(sql) → {Array}

Executes the given SQL statement. The result set is returned as JavaScript Array containing a JavaScript Object for each result.

##### Parameters:

Name | Type | Description
--- | --- | ---
`sql` | String | an SQL query statement

Source:
  - helma/Database.js, line 110

##### Returns:

an Array containing the result set

Type
  Array

#### (static) createInstance(driver, url, name, user, password) → {helma.Database}

Create a new Database instance using the given parameters.

Some of the parameters support shortcuts for known database products. The `url` parameter recognizes the values "mysql", "oracle" and "postgresql". For those databases, it is also possible to pass just `hostname` or `hostname:port` as `url` parameters instead of the full JDBC URL.

##### Parameters:

Name | Type | Description
--- | --- | ---
`driver` | String | the class name of the JDBC driver. As shortcuts, the values "mysql", "oracle" and "postgresql" are recognized.
`url` | String | the JDBC URL.
`name` | String | the name of the database to use
`user` | String | the the username
`password` | String | the password

Source:
  - helma/Database.js, line 261

##### Returns:

a helma.Database instance

Type
  helma.Database

#### (static) getInstance(name) → {helma.Database}

Get a Database instance using the Database source defined in the application's db.properties file with the given name.

##### Parameters:

Name | Type | Description
--- | --- | ---
`name` | String | the name of the DB source as defined in db.properties

Source:
  - helma/Database.js, line 308

##### Returns:

a helma.Database instance

Type
  helma.Database