In Linux, you can use occi to connect to oracle without installing the client. 1. Assume that you have successfully installed the oracle10g and ipvsec ++ environments. 2. Replace quot; opt
In Linux, you can use occi to connect to oracle without installing the client. 1. Assume that you have successfully installed the oracle10g and Eclipse C ++ environments. 2. Replace quot; opt
Use occi to connect to Oracle in Linux without installing a client
1. Assume that you have successfully installed the oracle10g and Eclipse C ++ environments.
2. Replace libocci. so.10.1 and libocci10.a in the "opt/.../db_1/lib" path. (First download the library required by occi, from which you can download the required two files)
3. edit/etc/ld. so. conf and enter/opt.../db_1/lib in the last line.
4. Run ldconfig
5.
For example
// Oracle. h (header file)
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace oracle: occi;
Class dbAccessor
{
Public:
DbAccessor ();
~ DbAccessor ();
Int get_dbstat ();
Bool Connect_DB ();
Void Disconn_DB ();
Bool ExecuteUpdate (const char * SQL );
ResultSet * ExecuteQuery (const char * SQL );
Void ClearQuery (ResultSet * set );
Void Init (const char * dbName, const char * dbUser, const char * dbPass );
Void SetDate (unsigned int paramIndex, time_t timeVal );
Private:
Environment * m_env;
Connection * m_conn;
Statement * m_stmt;
Const char * m_szUser;
Const char * m_szPassword;
Const char * m_szDbName;
Bool bConnected;
};
======= Oracle. cpp ====
# Include "oracle. h"
Using namespace std;
Using namespace oracle: occi;
DbAccessor: dbAccessor ()
{
BConnected = false;
M_env = 0;
M_conn = 0;
M_stmt = 0;
}
DbAccessor ::~ DbAccessor ()
{
Disconn_DB ();
}
Void dbAccessor: Init (const char * dbName, const char * dbUser, const char * dbPass)
{
M_szDbName = dbName;
M_szUser = dbUser;
M_szPassword = dbPass;
}
Bool dbAccessor: Connect_DB ()
{
If (m_env = 0)
M_env = Environment: createEnvironment ();
If (m_conn = 0 ){
Try {
M_conn = m_env-> createConnection (m_szUser, m_szPassword, m_szDbName );
} Catch (SQLException & ex ){
M_conn = 0;
Cout <"connect to database failed:" <ex. getMessage () <endl;
Return false;
}
}
Return true;
}
Void dbAccessor: isconn_DB ()
{
If (m_stmt! = 0 & m_conn! = 0)
M_conn-> terminateStatement (m_stmt );
If (m_conn! = 0 & m_env! = 0)
M_env-> terminateConnection (m_conn );
}
// Perform database update operations, including insert, update, and delete operations
Bool dbAccessor: ExecuteUpdate (const char * SQL)
{
Bool update_ OK = true;
If (SQL = 0 ){
Exit (1 );
}
If (m_conn = 0 & m_env = 0 ){
Exit (1 );
}
Try {
M_stmt = m_conn-> createStatement (SQL );
} Catch (SQLException & ex ){
M_stmt = NULL;
Cout <"ExecuteUpdate failed:" <ex. getMessage () <endl;
Exit (1 );
}
Update_ OK = m_stmt-> executeUpdate ();
Return update_ OK;
}
// Execute the database query operation, that is, the select Operation
ResultSet * dbAccessor: ExecuteQuery (const char * SQL)
{
ResultSet * set = 0;
If (SQL = 0 ){
Exit (1 );
}
If (m_conn = 0 & m_env = 0 ){
Exit (1 );
}
Try {
M_stmt = m_conn-> createStatement (SQL );
} Catch (SQLException & ex ){
M_stmt = NULL;
Cout <"ExecuteQuery failed:" <ex. getMessage () <endl;
Exit (1 );
}
Set = m_stmt-> executeQuery ();
Return set;
}
Void dbAccessor: ClearQuery (ResultSet * set)
{
If (m_stmt = 0 ){
Exit (1 );
}
If (set = 0 ){
Exit (1 );
}
M_stmt-> closeResultSet (set );
}
Void dbAccessor: SetDate (unsigned int paramIndex, time_t timeVal)
{
Cout < M_stmt = m_conn-> createStatement ();
Assert (m_stmt! = NULL );
Tm * timeStruct = localtime (& timeVal );
Date date (m_env,
TimeStruct-> maid + 1900,
TimeStruct-> tm_mon + 1,
TimeStruct-> tm_mday,
TimeStruct-> tm_hour,
TimeStruct-> tm_min,
(TimeStruct-> tm_sec = 60 )? 0: timeStruct-> tm_sec );
M_stmt-> setDate (paramIndex, date );
}