DOCUMENTS 5 - PortalScripting API
|
This class enables you to connect to a database and execute SQL statements. More...
Public Member Functions | |
boolean | close () |
Close the database connection and free the server ressources. More... | |
DBConnection | DBConnection () |
Connection to DOCUMENTS Database. More... | |
DBConnection | DBConnection (String connType, String connString, String user="", String password="") |
Connection to External Database. More... | |
DBResultSet | executeQuery (String sqlStatement) |
Execute a SELECT statement and retrieve a DBResultSet containing the result rows. More... | |
DBResultSet | executeQueryUC (String sqlStatement) |
Execute a SELECT statement using a x64/UTF-8 DOCUMENTS and retrieve a DBResultSet containing the result rows. More... | |
boolean | executeStatement (String sqlStatement) |
Execute any SQL statement on the external database. More... | |
boolean | executeStatementUC (String sqlStatement) |
Execute any SQL statement using a x64/UTF-8 DOCUMENTS on the external database. More... | |
String | getLastError () |
Function to get the description of the last error that occurred. More... | |
This class enables you to connect to a database and execute SQL statements.
You can connect to the DOCUMENTS database (see constructor without parameters) or to any external database (see constructor with parameters). For reading the result of a SELECT statement, the class DBResultSet is used. A DBConnection object can only be used for one DBResultSet object. Meaning, if you want to create another DBResultSet object you must also create an additional DBConnection object. Please consider the example in the constructor description.
DBConnection DBConnection::DBConnection | ( | ) |
Connection to DOCUMENTS Database.
The constructor without parameters can be used to create a connection to the database currently used in DOCUMENTS. For connecting an external database, see the documentation of the constructor with parameters.
DBConnection DBConnection::DBConnection | ( | String | connType, |
String | connString, | ||
String | user = "" , |
||
String | password = "" |
||
) |
Connection to External Database.
The constructor with parameters can be used to create a connection to an external database. Meaning to a database, that is not connected to DOCUMENTS. For connecting the DOCUMENTS database, we recommend to use the constructor without parameters.
You can connect to any database system of arbitrary type. But depending on your DOCUMENTS database, you can use different values for parameter connType
. The difference is described in the following two cases.
Case 1: Parameter connType
= "sqlserver" | "oracle" | "mysql"
This connection types allow you to connect to an external database of the given type. But the type must be identical to the database system that is connected to the DOCUMENTS server. DOCUMENTS supports the following types (also see system requirements).
So this parameter values can be used with the following database systems:
"mysql"
: MySQL and MariaDB "sqlserver"
: SQL Server "oracle"
: Oracle Database Case 2: Parameter connType
= "odbc"
This connection type allows the connection to an external database of almost every type. In this case the database will be accessed using the standard API for databases, called Open Database Connectivity (ODBC). The only restriction is, that an ODBC driver for the database must be available and this driver must be installed on the DOCUMENTS host. Additional, an ODBC Driver Manager can or sometimes must be used to specify a Data Source Name (DSN). The DSN allows you to define the required connection attributes all in one place. Sometimes, the credentials can also be specified at the DSN. If not, they can be set as parameters in the DBConnection constructor.
On DOCUMENTS 5.0e and lower the DSN must be used, if the DOCUMENTS database is not SQL Server.
ODBC
A short and good description about the common ODBC concept is available in the documentation of turbodbc (the ODBC documentation is independent of turbodbc). Additional there is a Microsoft documentation about ODBC.
ODBC on Windows
If your DOCUMENTS is running on Windows with SQL Server, you will probably have an ODBC Driver for SQL Server. Additional you should have the ODBC Data Source Administrator App for configuring Data Sources. To connect to a remote SQL Server, you may have to enable the TCP/IP protocol in SQL Server Configuration Manager and to open the port in the firewall on the remote SQL Server host.
ODBC on Linux
You can find installation instructions for the ODBC Driver for SQL Server in the Microsoft Documentation. The ODBC Driver Manager package unixODBC is available in the package archives of Ubuntu (e.g. 16.04 and 18.04) and can be installed using the package manager APT. For testing your database connection independent of PortalScripting, you can use isql.
connType | The type of the database connection ("odbc" | "sqlserver" | "oracle" | "mysql" ), see two cases in constructor description. |
connString | The complete connection string or the data source name (DSN). |
user | The login name used to authenticate against the external database. |
password | The (plaintext) password of the user utilized to connect to the database. |
null
in case of any error.boolean DBConnection::close | ( | ) |
Close the database connection and free the server ressources.
true
if successful, false
in case of any error DBResultSet DBConnection::executeQuery | ( | String | sqlStatement | ) |
Execute a SELECT statement and retrieve a DBResultSet containing the result rows.
sqlStatement | String containing the SELECT statement you want to execute in the database |
DBResultSet DBConnection::executeQueryUC | ( | String | sqlStatement | ) |
Execute a SELECT statement using a x64/UTF-8 DOCUMENTS and retrieve a DBResultSet containing the result rows.
sqlStatement | String containing the SELECT statement you want to execute in the database |
boolean DBConnection::executeStatement | ( | String | sqlStatement | ) |
Execute any SQL statement on the external database.
You can execute any SQL statement, as long as the database driver used for the connection supports the type of instruction. Use this method especially if you want to INSERT or UPDATE or DELETE data rows in tables of the external database. If you need to SELECT table rows, refer to the DBConnection.executeQuery() method.
true
if successful, false
in case of any error boolean DBConnection::executeStatementUC | ( | String | sqlStatement | ) |
Execute any SQL statement using a x64/UTF-8 DOCUMENTS on the external database.
You can execute any SQL statement, as long as the database driver used for the connection supports the type of instruction. Use this method especially if you want to INSERT or UPDATE or DELETE data rows in tables of the external database. If you need to SELECT table rows, refer to the DBConnection.executeQueryUC() method.
true
if successful, false
in case of any error String DBConnection::getLastError | ( | ) |
Function to get the description of the last error that occurred.