找不到具體的出處,,只好不寫了,。
OCCI數(shù)據(jù)庫ORACLE編程步驟
1. 配置環(huán)境
(1) Occi訪問數(shù)據(jù)庫需要occi.h頭文件,此文件在oracle安裝目錄下,,必須有oracle庫的支持,。安裝oracle服務端或客戶端;
(2) 配置NLS_LANG環(huán)境變量,,必須與數(shù)據(jù)庫的字符編碼一致,,否則可能出現(xiàn)中文亂碼,;
(3) 配置環(huán)境變量,使客戶端支持中文顯示,;
(4) 安裝C++編程工具和g++編譯工具,。
2. 編寫C++程序
(1) Environment類創(chuàng)建occi環(huán)境變量;
(2) Environment類下的createConnection方法創(chuàng)建連接數(shù)據(jù)庫的Connection對象,;
(3) Statement類創(chuàng)建對象,,由數(shù)據(jù)庫連接對象調(diào)用createStatement方法創(chuàng)建;
(4) Statement類創(chuàng)建對象調(diào)用setSQL()準備SQL語句,;
(5) Statement類創(chuàng)建對象調(diào)用execute()或executeUpdate()或executeQurey()或
executeArrayUpdate()方法執(zhí)行查詢,,如果是查詢的,創(chuàng)建ResultSet對象接收數(shù)據(jù)集,;如果是數(shù)據(jù)更新回插
入:Statement:: setAutoCommit (TRUE);
設置自動提交,;Statement::setAutoCommit(FALSE);
設置手動提交,手動提交:Connection::commit();Connection::rollback();
(6) Statement類創(chuàng)建對象調(diào)用getxxx()方法獲取字段數(shù)據(jù),;
(7) terminateStatement()終止Statement對象,;
(8) terminateConnection()斷開數(shù)據(jù)庫連接;
(9) Environment::terminateEnvironment()終止環(huán)境變量,。
例:
#include <iostream>
#include <occi.h>//occi頭文件
//聲明命名空間
using namespace oracle::occi;
using namespace std;
int main ()
{
Environment *env;
Connection *conn;
Statement *stmt;
ResultSet *rs;
string username = "system";//用戶名
string password = "st1234";//密碼
string connstring = "orcl";//數(shù)據(jù)庫連接字符串
string sql,strname;
int isno;
env = Environment::createEnvironment();//創(chuàng)建環(huán)境變量
conn =
env->createConnection(username,password,connstring);
//創(chuàng)建數(shù)據(jù)庫連接對象
stmt = conn->createStatement();//創(chuàng)Statement對象
sql = " SELECT u_id,u_name,to_char(u_date,'yyyy-mm-dd hh24:mi:ss')
FROM student";
stmt->setSQL(sql);//準備SQL語句
try
{
rs =
stmt->executeQuery();//執(zhí)行SQL語句,,返回結果集
while (rs->next()) //取數(shù)據(jù)
{
isno = rs->getInt(1);
strname=rs->getString(2);
cout<< isno
<< "
AND " << strname
<< endl;
cout<<rs->getString(3)<<endl;
}
cout <<"SELECT
SUCCESS" << endl;
}
catch(SQLException ex) //異常處理
{
cout << " Error Number
"<< ex.getErrorCode()
<< endl;