http://41062947.iteye.com/blog/2040784
方式1,使用OCCI:
直接上代码
#include <iostream> #include <string> #include <vector> #include <occi.h> using namespace oracle::occi; using std::vector; using namespace std; class conndba { private: Environment *env; Connection *conn; Statement *stmt; public: conndba(string user, string password, string db) { env = Environment::createEnvironment(Environment::DEFAULT); conn = env->createConnection(user, password, db); } ~conndba() { env->terminateConnection(conn); Environment::terminateEnvironment(env); } void insertBind(string s1, string s2, string s3, string s4) { string sqlStmt = "INSERT INTO t_user(userid, username, loginname, createdate) VALUES (:1, :2, :3, :4)"; stmt=conn->createStatement (sqlStmt); try { stmt->setString(1, s1); stmt->setString(2, s2); stmt->setString(3, s3); stmt->setString(4, s4); stmt->executeUpdate(); cout << "insert - Success" << endl; } catch (SQLException ex) { cout << "Exception thrown for insertBind" << endl; cout << "Error number: " << ex.getErrorCode() << endl; cout << ex.getMessage() << endl; } conn->terminateStatement(stmt); } void updateRow(string s1, string s2) { string sqlStmt = "UPDATE t_user SET userid = :x WHERE username = :y"; stmt = conn->createStatement(sqlStmt); try { stmt->setString(1, s2); stmt->setString(2, s1); stmt->executeUpdate(); cout << "update - Success" << endl; } catch (SQLException ex) { cout << "Exception thrown for updateRow" << endl; cout << "Error number: " << ex.getErrorCode() << endl; cout << ex.getMessage() << endl; } conn->terminateStatement(stmt); } void deleteRow(string s1) { string sqlStmt = "DELETE FROM t_user WHERE userid = :x"; stmt = conn->createStatement(sqlStmt); try { stmt->setString(1, s1); stmt->executeUpdate(); cout << "delete - Success" << endl; } catch (SQLException ex) { cout << "Exception thrown for deleteRow" << endl; cout << "Error number: " << ex.getErrorCode() << endl; cout << ex.getMessage() << endl; } conn->terminateStatement(stmt); } void displayAllRows() { string sql = "select userid, username, loginname, createdate from t_user"; stmt = conn->createStatement(sql); ResultSet *rs = stmt->executeQuery(); try { while (rs->next()) { cout << "userid: " << rs->getInt(1) << "\t" cout << "username: " << rs->getString(2) << "\t" cout << "loginname: " << rs->getString(3) << "\t" cout << "createdate: " << rs->getString(4) << endl; } } catch (SQLException ex) { cout << "Exception thrown for displayAllRows" << endl; cout << "Error number: " << ex.getErrorCode() << endl; cout << ex.getMessage() << endl; } stmt->closeResultSet(rs); conn->terminateStatement(stmt); } void displayAllRowsDesc() { string sql = "select userid, username, loginname, createdate from t_user order by userid desc"; stmt = conn->createStatement(sql); ResultSet *rs = stmt->executeQuery(); try { while (rs->next()) { cout << "userid: " << rs->getInt(1) << "\t" cout << "username: " << rs->getString(2) << "\t" cout << "loginname: " << rs->getString(3) << "\t" cout << "createdate: " << rs->getString(4) << endl; } } catch (SQLException ex) { cout << "Exception thrown for displayAllRows" << endl; cout << "Error number: " << ex.getErrorCode() << endl; cout << ex.getMessage() << endl; } stmt->closeResultSet(rs); conn->terminateStatement(stmt); } }; int main(int argc, char *argv[]) { string user = "UOP_ACT4"; string password = "123456"; string db = "hyacttst"; conndba *demo = new conndba(user, password, db); cout << "数据库中的记录:" << endl; demo->displayAllRowsDesc(); cout << "删除指定id的用户信息!" << endl; cout << "请输入需要删除的用户id:" << argv[1] << endl; demo->deleteRow(argv[1]); cout << "删除指定id的用户信息后!" << endl; demo->displayAllRows(); cout << "调用析构函数进行处理!"; delete (demo); }
方式2:使用OCI,我们项目中正在使用的方式,看起来及其复杂。
1 #include <oci.h> 2 3 #include <iostream> 4 5 #include <string> 6 7 #include <string.h> 8 9 #include <stdlib.h> 10 11 using namespace std; 12 13 //存放查询数据的结构体 14 15 struct result 16 17 { 18 19 char ename[20]; 20 21 char cname[20]; 22 23 result() 24 25 { 26 27 memset(ename, '\0', sizeof(ename)); 28 29 memset(cname, '\0', sizeof(cname)); 30 31 } 32 33 }; 34 35 int main() 36 37 { 38 39 // 初始化 OCI 环境句柄指针 40 41 OCIEnv *envhpp = NULL; 42 43 // 初始化服务器句柄 44 45 OCIServer *servhpp = NULL; 46 47 // 用于捕获 OCI 错误信息 48 49 OCIError *errhpp = NULL; 50 51 // 初始化会话句柄 52 53 OCISession *usrhpp = NULL; 54 55 // 初始化服务上下文句柄 56 57 OCISvcCtx *svchpp = NULL; 58 59 // 初始化表达式句柄 60 61 OCIStmt *stmthpp = NULL; 62 63 string server = "mydb"; 64 65 // 创建 OCI 环境 , 并设置环境句柄。 66 67 sword swResult = OCIEnvCreate(&envhpp, OCI_DEFAULT, NULL, NULL, NULL, NULL, 0, NULL); 68 69 if (swResult != OCI_SUCCESS && swResult != OCI_SUCCESS_WITH_INFO) 70 71 { 72 73 cout 《 "Oracle environment initialization error!" 《 endl; 74 75 exit(1); 76 77 } 78 79 cout 《 "Oracle environment initialization success!" 《 endl; 80 81 // 创建错误句柄 82 83 OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&errhpp, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0); 84 85 // 创建服务句柄 86 87 OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&servhpp, OCI_HTYPE_SERVER, (size_t)0, (dvoid **)0); 88 89 // 连接服务器,如果失败则获取错误码 90 91 if (OCIServerAttach(servhpp, errhpp, (text *)server.c_str(), strlen(server.c_str()), 0) != OCI_SUCCESS) 92 93 { 94 95 int errcno; 96 97 char errbuf[512] = ""; 98 99 sb4 errcode; 100 101 // 获取错误指针和 OCI 错误代码 102 103 OCIErrorGet((dvoid *)errhpp, (ub4)1, (text *)NULL, &errcode, (ub1 *)errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR); 104 105 errcno = errcode; 106 107 cout 《 "Oracle server attach error:" 《 errbuf 《 endl; 108 109 OCIHandleFree((dvoid *)envhpp,OCI_HTYPE_ENV); 110 111 OCIHandleFree((dvoid *)servhpp,OCI_HTYPE_SERVER); 112 113 OCIHandleFree((dvoid *)errhpp,OCI_HTYPE_ERROR); 114 115 exit(1); 116 117 } 118 119 cout 《 "Oracle server attach success!"《 endl; 120 121 /***************** 连接数据库 ****************/ 122 123 string user = "user"; 124 125 string pas = "passwd"; 126 127 errhpp = NULL; 128 129 // 创建错误句柄 130 131 (void) OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&errhpp, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0); 132 133 // 创建服务上下文句柄 134 135 (void) OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&svchpp, OCI_HTYPE_SVCCTX, (size_t) 0, (dvoid **)0); 136 137 // 设置属性 138 139 (void) OCIAttrSet((dvoid *)svchpp, OCI_HTYPE_SVCCTX, (dvoid *)servhpp, (ub4)0, OCI_ATTR_SERVER, (OCIError *)errhpp); 140 141 // 创建用户连接句柄 142 143 (void) OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&usrhpp, (ub4)OCI_HTYPE_SESSION, (size_t) 0, (dvoid **)0); 144 145 // 设置用户名、密码 146 147 (void) OCIAttrSet((dvoid *)usrhpp, (ub4)OCI_HTYPE_SESSION, (dvoid *)user.c_str(), (ub4)strlen(user.c_str()), (ub4)OCI_ATTR_USERNAME, errhpp); 148 149 (void) OCIAttrSet((dvoid *)usrhpp, (ub4)OCI_HTYPE_SESSION, (dvoid *)pas.c_str(), (ub4)strlen(pas.c_str()), (ub4)OCI_ATTR_PASSWORD, errhpp); 150 151 // 创建会话连接 152 153 if(OCISessionBegin(svchpp, errhpp, usrhpp, OCI_CRED_RDBMS, (ub4)OCI_DEFAULT) != OCI_SUCCESS) 154 155 { 156 157 int errcno; 158 159 char errbuf[512]= 160 { '\0'}; 161 162 sb4 errcode; 163 164 // 获取错误指针和 OCI 错误代码 165 166 OCIErrorGet((dvoid *)errhpp, (ub4)1, (text *)NULL, &errcode, (ub1 *)errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR); 167 168 errcno = errcode; 169 170 cout 《 "User session error:" 《 errbuf 《 endl; 171 172 OCIHandleFree((dvoid *)errhpp,OCI_HTYPE_ERROR); 173 174 OCIHandleFree((dvoid *)usrhpp,OCI_HTYPE_SESSION); 175 176 OCIHandleFree((dvoid *)svchpp,OCI_HTYPE_SVCCTX); 177 178 exit(1); 179 180 } 181 182 cout 《 "user session success!" 《 endl; 183 184 (void) OCIAttrSet((dvoid *)svchpp, (ub4) OCI_HTYPE_SVCCTX, (dvoid *)usrhpp, (ub4)0, (ub4)OCI_ATTR_SESSION, errhpp); 185 186 /***********************以上代码已经成建立了一条数据库连接************************/ 187 188 /*************** 以下代码是对数据库的各种操作 *** 执行 查询SQL 语句 ******************/ 189 190 errhpp = NULL; 191 192 // 创建一个表达式句柄 193 194 if (OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&stmthpp, OCI_HTYPE_STMT, (size_t)0, (dvoid **)0) != OCI_SUCCESS) 195 196 { 197 198 cout 《 "Create STMT error !" 《 endl; 199 200 exit(1); 201 202 } 203 204 cout 《 "Create stmt success !" 《 endl; 205 206 // 创建错误句柄 207 208 OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&errhpp, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0); 209 210 // Select语句 211 212 char sql[255] = "select col1, col2 from table1 "; 213 214 if (OCIStmtPrepare(stmthpp, errhpp, (text *)sql, (ub4)strlen(sql), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT) != OCI_SUCCESS) 215 216 { 217 218 cout 《 "Create prepare error!" 《 sql 《 endl; 219 220 exit(1); 221 222 } 223 224 cout 《 "Create prepare success!" 《 endl; 225 226 /********* 绑定参数 ***********/ 227 228 // 申请绑定字段的句柄 229 OCIDefine *bhp1 = NULL; 230 231 OCIDefine *bhp2 = NULL; 232 233 // 存放数据的结构 234 235 struct result rst; 236 237 // 指定提取数据长度 238 239 ub2 datalen = 0; 240 241 // 定义指示器变量 , 用于取可能存在空值的字段 242 243 char isnul[6] = ""; 244 245 // 定义输出变量 , 246 247 OCIDefineByPos(stmthpp, &bhp1, errhpp, 1, (dvoid *)&rst.ename, sizeof(rst.ename), SQLT_CHR, NULL, &datalen, NULL, OCI_DEFAULT); 248 249 OCIDefineByPos(stmthpp, &bhp2, errhpp, 2, (dvoid *)&rst.cname, sizeof(rst.cname), SQLT_STR, NULL, &datalen, NULL, OCI_DEFAULT); 250 251 // 获取 SQL 语句类型 252 253 ub2 stmt_type; 254 255 OCIAttrGet ((dvoid *)stmthpp, (ub4)OCI_HTYPE_STMT, (dvoid *)&stmt_type, (ub4 *)0, (ub4)OCI_ATTR_STMT_TYPE, errhpp); 256 257 // 执行 SQL 语句 258 259 OCIStmtExecute(svchpp, stmthpp, errhpp, (ub4)0, (ub4)0, (OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DEFAULT); 260 261 // 获取查询信息 262 263 int rows_fetched; 264 265 do 266 267 { 268 269 cerr 《 rst.ename《 " "; 270 271 cerr 《 rst.cname《 " \n"; 272 273 } 274 275 while(OCIStmtFetch2(stmthpp, errhpp, 1, OCI_FETCH_NEXT, 1, OCI_DEFAULT) != OCI_NO_DATA); 276 277 // 获得记录条数 278 279 OCIAttrGet((CONST void *)stmthpp, OCI_HTYPE_STMT, (void *)&rows_fetched, (ub4 *)sizeof(rows_fetched), OCI_ATTR_ROW_COUNT, errhpp); 280 281 cout 《 " rows :" 《 rows_fetched 《 endl; 282 283 /*************** 执行 新增SQL 语句 ******************/ 284 285 if (OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&stmthpp, OCI_HTYPE_STMT, (size_t)0, (dvoid **)0) != OCI_SUCCESS) 286 287 { 288 289 cout 《 "Create STMT error !" 《 endl; 290 291 exit(1); 292 293 } 294 295 cout 《 "Create stmt success !" 《 endl; 296 297 OCIHandleAlloc((dvoid *)envhpp, (dvoid **)&errhpp, OCI_HTYPE_ERROR, (size_t)0, (dvoid **)0); 298 299 // Insert语句 300 301 char sql2[255] = "insert into table1 (col1, col2) values('testoci', 'testoci')"; 302 303 // 准备Sql语句 304 305 if (OCIStmtPrepare(stmthpp, errhpp, (text *)sql2, (ub4)strlen(sql2), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT) != OCI_SUCCESS) 306 307 { 308 309 cout 《 "Create prepare error!" 《 sql2 《 endl; 310 311 exit(1); 312 313 } 314 315 cout 《 "Create prepare success!" 《 endl; 316 317 // 执行SQL 语句 318 319 OCIStmtExecute(svchpp, stmthpp, errhpp, (ub4)1, (ub4)0, (OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_DEFAULT); 320 321 // 断开用户会话 322 323 OCILogoff(svchpp, errhpp); 324 325 // 断开服务器连接 326 327 OCIServerDetach(servhpp, errhpp, OCI_DEFAULT); 328 329 // 释放资源 330 331 OCIHandleFree((dvoid *) stmthpp, OCI_HTYPE_STMT); 332 333 OCIHandleFree((dvoid *) svchpp, OCI_HTYPE_SVCCTX); 334 335 OCIHandleFree((dvoid *) servhpp, OCI_HTYPE_SERVER); 336 337 OCIHandleFree((dvoid *) errhpp, OCI_HTYPE_ERROR); 338 339 return 0; 340 341 }
没有编译验证, 如果有错请看的童鞋多多包含。
明显OCCI是OCI的进阶版,个人认为,在硬件不是制约条件的今天,OCCI和OCI的执行效率不差上下,但是从程序员的调用方便来讲,明显OCCI要方便的多,如果以后的项目还要用到的话,本人会毫不犹豫的选择OCCI。