Using OCCI to connect to the Oracle database on windows, occioracle
In this section, we try to use OCCI to connect to the Oracle database on windows.
Oracle C ++ CallInterface (OCCI) is an application programming interface provided by Oracle. It allows C ++ programs to interact with one or more Oracle databases, although OCI is more difficult to develop, it is extremely fast and is an underlying interface that can manipulate almost any object in the Oracle database.
Environment: windows7 64bit
IDE: V. S. 2010
Server: winserver2008 Oracle 12c
Download the SDK:
First download the corresponding Cilent development kit on the Oracle official website, which contains some necessary library files. Be sure to download the client development kit of the corresponding version. I put my development kit under drive D.
Set environment variables:
Choose computer> right-click Properties> advanced system Settings> environment variables to set system variables.
Path = D :\ instantclient-basic-nt-12.1.0.2.0 \ instantclient_12_1
NLS_LANG = AMERICAN_AMERICA.ZHS16GBK
Configure VS2010:
First create an empty project OCCIConnectOracle
1. Add the library file directory:
Solution right-click OCCIConnectOracle-> properties-> c/c ++-> General-> additional include directory, enter library directory address: D: \ instantclient-sdk-nt-12.1.0.2.0 \ instantclient_12_1 \ sdk \ include
2. Add the header file directory:
Property-> linker-> General-> additional library Directory: D: \ instantclient-sdk-nt-12.1.0.2.0 \ instantclient_12_1 \ sdk \ lib \ msvc \ vc10
3. Add a library file:
Property-> linker-> input-> additional dependency: There are two files, oraocci12.lib and oraocci12d. lib, which are used when oraocci12.lib is in release mode and oraocci12d. lib is used in debug mode.
Code Section:
[Cpp] view plain copy print?
/*************************** Using oracle itself to provide OCCI methods for database operations **** ***************/
// The purpose of the code is to verify that the oracle header file and lib file path in makefile are correct.
# Include
# Define WIN32COMMON // avoid function redefinition errors
# Include
Using namespace std;
Using namespace oracle: occi;
Int main ()
{
System ("pause ");
// Create an OCCI Context
Environment * env = Environment: createEnvironment ();
If (NULL = env ){
Printf ("createEnvironment error. \ n ");
Return-1;
}
Else
Cout <"success" <endl;
String name = "system ";
String pass = "123 ";
String srvName = "192.168.26.74: 1521/orcl ";
Try
{
// Create a database connection
Connection * conn = env-> createConnection (name, pass, srvName); // user name, password, Database name
If (NULL = conn ){
Printf ("createConnection error. \ n ");
Return-1;
}
Else
Cout <"conn success" <endl;
// Data operation to create a Statement object
Statement * pStmt = NULL; // Statement object
PStmt = conn-> createStatement ();
If (NULL = pStmt ){
Printf ("createStatement error. \ n ");
Return-1;
}
// Query the database time
Std: string strTemp;
ResultSet * pRs = pStmt-> executeQuery (
"SELECT TO_CHAR (SYSDATE, 'yyyy-MM-DD HH24: MI: ss') from dual ");
While (pRs-> next ()){
StrTemp = pRs-> getString (1 );
Printf ("db time: % s. \ n", strTemp. c_str ());
// Use getInt () for int type values ()
Break;
}
PStmt-> closeResultSet (pRs );
// --- Insert ---
// Specify DML for automatic submission
PStmt-> setAutoCommit (TRUE );
// Set the SQL statement to be executed
// PStmt-> setSQL ("insert into ta (ID, NAME) VALUES (1, 'zs ')");
PStmt-> setSQL ("insert into TABLE_TEST_WANG (NAME, NUM, AGE) VALUES ('deng Chao ', '99', '41 ')");
// Execute the SQL statement
Unsigned int nRet = pStmt-> executeUpdate ();
If (nRet = 0 ){
Printf ("executeUpdate insert error. \ n ");
}
// Terminate the Statement object
Conn-> terminateStatement (pStmt );
// Close the connection
Env-> terminateConnection (conn );
// PEnv-> terminateConnection (pConn );
}
Catch (SQLException e)
{
Cout <e. what () <endl;
System ("pause ");
Return-1;
}
// Release the OCCI Context
Environment: terminateEnvironment (env );
Cout <"end !" <Endl;
System ("pause ");
Return 0;
} <