#include <occi.h>
using namespace oracle::occi; using namespace std;
int main () {
Environment *env; Connection *conn; Statement *stmt;
string username = “ipd”; string password = “ipd”; string connstring = “ora9i”; string sql;
env = Environment::createEnvironment(); //创建一个环境变量
conn = env->createConnection(username,password,connstring); //创建一个数据库连接对象 stmt = conn->createStatement(); //创建一个Statement对象
sql = “ INSERT INTO student (sno,sname) VALUES ( :x,:y) “; //拼接SQL语句 stmt->setSQL(sql); //设置SQL语句到Statement对象中 try { stmt->setInt(1,10001); //给第一个参数x赋值 stmt->setString(2,”zhangsan”); //给第二个参数y赋值 stmt->executeUpdate(); //执行SQL语句 cout << “INSERT ―― SUCCESS” << endl; } catch (SQLException ex) { cout << “ Error Number : “<< ex.getErrorCode() << endl; //获得异常代码 cout << ex.getMessage() << endl; //获得异常信息 }
conn->terminateStatement(stmt); //终止Statement对象 env->terminateConnection(conn); //断开数据库连接
Environment::terminateEnvironment(env); //终止环境变量
return 1; }
5.3. 批量处理非查询语句
在批量处理非查询语句时,首先必须使用m_stmt->setMaxIterations(unsigned int maxnum)方法设置最大的批处理数;然后使用setMaxParamSize(unsigned int paramindex,int paramsize)方法设置对应参数的大小,对于有固定长度的参数可以不必重新设置,如int,double;最后