我创建了一个简单的C++程序来连接Oracle OCCI和OracleDB实例。
这是代码:
#include <iostream>
#include <string>
#include <occi.h>
// Connection information
const std::string userName = "admin";
const std::string password = "password";
const std::string connectString = "(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.8.190)(PORT = 1521)))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = ORCLCDB)))";
// Connection enviroment
oracle::occi::Environment *env = oracle::occi::Environment::createEnvironment();
int main() {
int succes;
oracle::occi::Connection *conn = env->createConnection(userName, password, connectString);
succes = 0;
std::string id;
std::cin >> id;
std::string anw = "SELECT * FROM BESTAND WHERE ID=";
std::string cnw = anw + id;
oracle::occi::Statement *stmt = conn -> createStatement(cnw);
oracle::occi::ResultSet *rs = stmt -> executeQuery();
while (rs -> next()) {
std::string b = rs -> getString(2);
std::cout << b << std::endl;
}
stmt -> closeResultSet(rs);
conn -> terminateStatement(stmt);
env -> terminateConnection(conn);
oracle::occi::Environment::terminateEnvironment(env);
}
我在CentOS-8上使用g++来编译程序,程序编译没有问题,但当我执行程序时,我得到了这个错误:
terminate called after throwing an instance of 'oracle::occi::SQLException'
what(): ORA-12162: TNS:net service name is incorrectly specified
这里有人知道我该怎么解决这个问题吗。
"ORCLDB"是SERVICE_NAME还是ORACLE_SID?服务名称通常与域完全限定。你可以试试这个(空格很好(:
(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.8.190)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED)(SID = ORCLCDB)))
或者从TNS侦听器状态中仔细检查服务名称,以确保获得正确的值。数据库服务器上的"lsnrctl-status"命令应该返回活动服务的列表;使用状态为"就绪"的。