//存储过程
create or replace procedure ADDDDD(
pid in number,
pname in VARCHAR2
) is
begin
INSERT INTO studentto VALUES(pid,pname);
end ADDDDD;
#include <stdio.h>
#include <stdlib.h>#include <iostream>
#define LINUXOCCI
#include <occi.h>
#include <list>
#include <iterator>
#include <iomanip>
#include <string.h>
using namespace std;
using namespace oracle::occi;
class TestCallPRO
{
public:
TestCallPRO(std::string userName,std::string passWD,std::string serverName);//参数初始化
~TestCallPRO();
void LinkOnOracle();//链接数据库
void LinkOffOracle();//断开数据库的链接
void SpliceSQL(std::list<char*> &cValues); //拼接SQL语句(insert 的存储过程)
void CValuesToList(char* &p1,char *&p2,std::list<char*> &cValues); //参数入栈
void ExecuteSQlProc(std::list<char*> &cValues);//调用执行存储过程
protected:
Environment *m_env;
Connection *m_conn;
Statement *m_pstmt;
std::string m_userName;//数据库用户名
std::string m_passWD;//用户密码
std::string m_serverName; //格式: <服务器IP>:<监听端口>/<数据库名>
};
TestCallPRO::TestCallPRO(std::string userName,std::string passWD,std::string serverName)
{
m_env = NULL;
m_conn = NULL;
m_pstmt = NULL;
m_userName = userName;
m_passWD = passWD;
m_serverName = serverName;
}
void TestCallPRO::LinkOnOracle()
{
m_env=Environment::createEnvironment();//创建数据库连接对象
m_conn = m_env->createConnection(m_userName,m_passWD,m_serverName);//Link
}
void TestCallPRO::LinkOffOracle()
{
m_env->terminateConnection(m_conn);
Environment::terminateEnvironment(m_env);
}
void TestCallPRO::SpliceSQL(std::list<char*> &cValues)
{
std::string astrSQL = "";
astrSQL = "call ";
astrSQL += "ADDDDD";
astrSQL += "(";
char suu[2] ={ 'x','\0'};
std::list<char *>::iterator loIter = cValues.begin();
for(; loIter != cValues.end(); loIter++)
{
std::string str = "";
str = &suu[0];
astrSQL += ":";
astrSQL += str;
astrSQL += ",";
++ suu[0];
}
if(astrSQL[astrSQL.length() - 1] == '(')
{
astrSQL[astrSQL.length() - 1] = ';';
}
else
{
astrSQL[astrSQL.length() - 1 ] = ')';
}
m_pstmt = m_conn->createStatement();//执行非查询语句 不需要传递SQL语句
m_pstmt->setSQL(astrSQL);
}
void TestCallPRO::CValuesToList(char* &p1,char *&p2,std::list<char*> &cValues)
{
cValues.push_back(p1);
cValues.push_back(p2);
}
void TestCallPRO::ExecuteSQlProc(std::list<char*> &cValues)
{
m_pstmt->setAutoCommit(false);
m_pstmt->setMaxParamSize(1,sizeof(int));
m_pstmt->setMaxParamSize(2,sizeof(string));
char *p1 = NULL;
int itemp = 0;
try
{
std::list<char *>::iterator iter = cValues.begin();
itemp = atoi((*iter));
cout<<"begin 1"<<endl;
m_pstmt->setInt(1, itemp);
cout<<"begin 2"<<endl;
iter++;
p1 = new char[strlen((*iter))+1];
memcpy(p1,(*iter),strlen((*iter)));
m_pstmt->setString(2, p1);
}
catch(SQLException ex)
{
cout<<ex.getErrorCode()<<endl;
}
m_pstmt->executeUpdate();//执行非查询语句
m_conn->commit();
cout<<"OK!!!!"<<endl;
}
TestCallPRO::~TestCallPRO()
{
}
int main(void)
{
int iID = 1521;
char *p1 = new char[sizeof(int) +1];
memset(p1, 0, sizeof(int) +1);
sprintf(p1,"%d",iID);
char *p2 = new char[10];
strcpy(p2, "name");
std::list<char *> aoStoParam;
TestCallPRO testcallPRO("xxxxx","xxxxx","xx.xx.xx.xx:xx/xxxx");
testcallPRO.LinkOnOracle();
testcallPRO.CValuesToList(p1, p2, aoStoParam);
testcallPRO.SpliceSQL(aoStoParam);
testcallPRO.ExecuteSQlProc(aoStoParam);
testcallPRO.LinkOffOracle();
return 0;
}