SQL 与数据库的增删改查

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#include
#include"occi.h"
using namespace std;
using namespace oracle::occi;
class Dml{
private:
Environment *env;
Connection *conn;
Statement *stmt;
public:
Dml();
Dml(string,string,string);
~Dml();
void add(int,string,string,int,float);
void del(int);
void update(int,string,string,int,float);
void list();
};
Dml::Dml(){
}
//通过带参数的构造函数进行初始华
//环境变量的创建和链接的创建
Dml::Dml(string username,string userpass,string connstr){
env=Environment::createEnvironment(Environment::OBJECT);
conn=env->createConnection(username,userpass,connstr);
if(conn){
cout<<"success..."<}else{
cout<<"faliure"<}
}
//用于销毁链接以及环境对象
Dml::~Dml(){
//先销毁conn
env->terminateConnection(conn);
//销毁env
Environment::terminateEnvironment(env);
}
void Dml::add(int userid,string username,string usersex,
int userage,float usersalary){
//创建sql语句
string sqlstr="insert into tb_user values(:1,:2,:3,:4,:5)";
//创建sql语句的执行对象
stmt=conn->createStatement();
try{
//给参数赋值
stmt->setSQL(sqlstr);
stmt->setInt(1,userid);
stmt->setString(2,username);
stmt->setString(3,usersex);
stmt->setInt(4,userage);
stmt->setFloat(5,usersalary);
//执行sql语句
stmt->executeUpdate();
}catch(SQLException e){
cout<<"exception:"<}
//销毁stmt对象
conn->terminateStatement(stmt);
}
void Dml::del(int userid){
string sqlstr="delete from tb_user where user_id=:1";
stmt=conn->createStatement();
try{
stmt->setSQL(sqlstr);
stmt->setInt(1,userid);
stmt->executeUpdate();
}catch(SQLException e){
cout<}
conn->terminateStatement(stmt);

}
void Dml::list(){
string sqlstr="select * from tb_user";
stmt=conn->createStatement();
try{
stmt->setSQL(sqlstr);
//获取对应的结果基
ResultSet *rs=stmt->executeQuery();
//遍历结果集
while(rs->next()){
//取到结果集中的每条记录
int userid=rs->getInt(1);
string username=rs->getString(2);
string usersex=rs->getString(3);
int userage=rs->getInt(4);
float usersalary=rs->getFloat(5);
cout<userage<<"\t"<}
//关闭结果集
stmt->closeResultSet(rs);

}catch(SQLException e){
cout<<"exception:"<
}
conn->terminateStatement(stmt);
}
void Dml::update(int userid,string username,string usersex,
int userage,float usersalary){
string sqlstr="update tb_user set user_name=:1,user_sex=:2,"
"user_age=:3,user_salary=:4,where user_id=:5";
try{
stmt->setSQL(sqlstr);
stmt->setString(1,username);
stmt->setString(2,usersex);
stmt->setInt(3,userage);
stmt->setFloat(4,usersalary);
stm

t->setInt(5,userid);
stmt->executeUpdate();

}catch(SQLException e){
cout<}
conn->terminateStatement(stmt);
}
int main()
{ char a[100]={'\0'};
char b[100]={'\0'};
cout<<"输入用户"<cin>>a;
cout<<"输入密码"<cin>>b;
if((strcmp(a,"softeem")==0)&&(strcmp(b,"softeem")==0))
{
string username="suncs";
string userpass="suncs";
string connstr="192.168.113.115:1521/orcl";
Dml *d=new Dml(username,userpass,connstr);
//d->add(9,"zhu","e",22,90);
//d->add(5,"haha","e",28,80);
d->list();
//d->del(9);
//d->list();
delete d;
}else{
cout<<"登录失败"<}
memset(&a,0x00,sizeof(a));
memset(&b,0x00,sizeof(b));
return 0;
}

相关文档
最新文档