|
|
|
OCL has special class to establish and manage a connection. This class is OraConnection. All OCL objects which interact with the database server require OraConnection object. To use OraConnection and other OCL classes you have to include ocl.h header file.
#include "ocl.h"
Then you declare an instance of the OraConnection class.
OraConnection connection;
Before connecting to Oracle you need to set up login information with setUsername, setPassword and setServer member functions. Remember to specify an existing TNS alias when calling setServer method.
connection.setUsername("scott");
connection.setPassword("tiger");
connection.setServer("ora");
To establish a connection call connect member function.
connection.open();
Now you may execute any SQL or PL/SQL statement, obtain results from queries or call stored procedures. This is accomplished by first associating objects of OraCommand or OraQuery classes with the OraConnection instance through their setConnection member functions or in constructor.
OraCommand cmd;
cmd.setConnection(connection);
When you finished accessing database close the connection by calling disconnect member function.
connection.close();
OraConnection allows you to control transactions in your programs. To explicitly start new transaction use startTransaction method. With the commit function you can afterwards finish the transaction and make all changes permanent. Or you may use the rollback function to drop data changes you have made for the current transaction.
Example
OraConnection connection();
connection.setUsername("scott");
connection.setPassword("tiger");
connection.setServer("ora");
connection.open();
. . .
connection.startTransaction();
. . .
connection.commit();
. . .
connection.rollback();
. . .
connection.close();
See Also
OraConnection, OraCommand, OraQuery
OCL | Using OCL | Index