oracle存储过程的调用(insert 语句的存储过程)

//存储过程

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值