方式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) <&l