Linux c++ 连接Oracle

1.在已经安装Oracle的linux系统上安装

instantclient-basic-linux.x64-11.2.0.3.0.zip

2.unzipit,copylibocci.soandlibclntsh.soto/libor/usr/lib

3.runldconfig

4.

#include <iostream>  
#include <string>  
#include "occi.h"

using namespace oracle::occi;  
using namespace std;  
    
int main()  
    {  
            string usr="test";  
            string pwd="t2npRh6Oq5su5dBnS4VrpHIYLKv+";  
            string SID="test";  
            string date;  
      
            Environment *env=Environment::createEnvironment(Environment::OBJECT);  
            Connection *conn= env->createConnection(usr,pwd,SID);//all strings  
            if(conn)  
                    cout<<"success createConnection!"<<endl;  
            else  
                    cout<<"failure createConnection!"<<endl;  
           
            Statement *stmt = conn->createStatement();  
            string sSQL = "select to_char(enddate,'yyyy-mm-dd hh24:mi:ss') from info";  
            stmt->setSQL(sSQL);  
      
      
            ResultSet *rs = stmt->executeQuery();  
            if(rs->next())  
            {  
                    date = rs->getString(1);  
            }  
      
            cout<<"now time :"<<date<<endl;  
            
            env->terminateConnection(conn);  
            Environment::terminateEnvironment(env);  
           
            return 0;  
    }

compile

g++ main.cpp -o test -I/u01/app/oracle/product/11.2.0/dbhome_1/rdbms/public/ -L/u01/app/oracle/product/11.2.0/dbhome_1/rdbms/lib

Makefile

INCDIR=-I$(ORACLE_HOME)/rdbms/public/
LDFLAGS=-L$(ORACLE_HOME)/rdbms/lib/ -lclntsh -locci
COMPILECMD=g++
targets := clean


.PHONY: all clean

all:clean
	@echo "run make all "
	$(COMPILECMD) main.cpp -o test $(INCDIR) $(LDFLAGS) 
clean:
	@echo "run make clean "
	rm -rf test

相关推荐