程序中使用system调用sqlplus命令来检查是否可以连接

 测试system命令的使用,如果命令不正确,如system("ddddd"); 返回值为1

如果命令本身正确,无论执行中是否正确,如system("sqlplus hr/hr"); 正确的用户名和密码  system("sqlplus hr/hrtest"); 不正确的用户名和密码,system的返回值都为0,这种情况下根据这个返回值不能作判断,

把显示的结果重定向到文件中,可以分析文件内容来判断

根据文件中的关键词ERROR 或者 ORA- 来判断是否出错,这种方法是可行的。

 通过这种方式,就可以实现程序方法来替代手工操作来判断数据库是否可以连接。

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 int i = system("echo exit | sqlplus hr/hr > data.txt");
 cout << "i = " << i << endl;//0-success

  i = system("echo exit | sqlplus hkkr/hr > d2.txt");
 cout << "i = " << i << endl;//0-success i also 0 return

 return 0;
}

 

 

data.txt


SQL*Plus: Release 11.2.0.1.0 Production on 星期二 1月 8 00:39:14 2013

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> 从 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options 断开

 

 

d2.txt


SQL*Plus: Release 11.2.0.1.0 Production on 星期二 1月 8 00:39:18 2013

Copyright (c) 1982, 2010, Oracle.  All rights reserved.

ERROR:
ORA-01017: invalid username/password; logon denied


请输入用户名:  输入口令: ERROR:
ORA-01005: 给出空口令; 登录被拒绝


请输入用户名

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值