linux中用netbeans开发ocilib连接oracle程序

开发环境

Ubuntu9netbeans6.9

所需下载内容:

instantclient-basic-linux32-11.2.0.2.0.zip

instantclient-sdk-linux32-11.2.0.2.0.zip

ocilib-3.7.0-gnu.zip

参考网址

http://orclib.sourceforge.net/doc/html/group__g__install.html

ubuntunetbeans中配置c++开发环境

确保ubuntu中有c++编译器,如果没有则用sudo apt-get install build-essential安装。

进入netbeans安装如下界面配置

 

可以在main.cppprintf("hello world!")测试标准库及环境是否配置好,注意如果要打印中文则工程最好用utf-8编码,因为ubuntu的终端默认编码为utf-8,也可以都相应改为GBK(工程、终端编码)

配置oracle即时客户端

/etc/profile.d中新建一个oracle.sh文件,内容为:

export ORACLE_HOME=/opt/soft/instantclient_11_2

export PATH=$ORACLE_HOME:$PATH

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME

export TNS_ADMIN=$ORACLE_HOME/network/admin

export NLS_LANG="Simplified Chinese_china".ZHS16GBK

$ORACLE_HOME/network/admin新建一个tnsnames.ora文件:

TMS=

  (DESCRIPTION =

    (ADDRESS_LIST =

      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.77.62)(PORT = 1521))

    )

    (CONNECT_DATA =

      (SERVICE_NAME = orcl)

    )

  )

编译ocilib

注意$ORACLE_HOME中是否有libclntsh.so链接文件,如果没有则创建:

 $ ln -s $ORACLE_HOME/libclntsh.so.10.1 $ORACLE_HOME/libclntsh.so

然后在控制台中编译ocilib

 $ cd ocilib-x.y.z

 $ ./configure --with-oracle-headers-path=/opt/soft/instantclient_11_2/include --with-oracle-lib-path=/opt/soft/instantclient_11_2

 $ make

 $ make install

默认在/usr/local/lib生成有libocilib.alibocilib.so

$ldconfig #让动态链接库为系统所共享 

配置ocilib链接库

/usr/local/lib中的libocilib.a添加到项目中

连接oracle

    注意在oracle.shexport NLS_LANG="Simplified Chinese_china".ZHS16GBK

说明要连接的oracle的编码为GBK

ocilib.h加入到项目的头文件,注意其文件的编码要与项目一致。

输入如下代码进行连接测试:

#include <stdlib.h>

#include <stdio.h>

#include <ocilib.h>

int main(int argc, char** argv) {

   OCI_Connection *cn;

   OCI_Statement *st;

   OCI_Resultset *rs;

   int i, n;

   printf("set terminate charset to GBK:");

   char input;

   scanf(&input);

    if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT))

        return EXIT_FAILURE;

    cn = OCI_ConnectionCreate("TMS", "tms", "jpsoft", OCI_SESSION_DEFAULT);

    printf("Server major    version : %i/n",   OCI_GetServerMajorVersion(cn));

    printf("Server minor    version : %i/n",   OCI_GetServerMinorVersion(cn));

    printf("Server revision version : %i/n/n", OCI_GetServerRevisionVersion(cn));

    printf("Connection      version : %i/n/n", OCI_GetVersionConnection(cn));

    st = OCI_StatementCreate(cn);

    OCI_ExecuteStmt(st, "SELECT * from comm_log where rownum<10");

    rs = OCI_GetResultset(st);

    printf("中文显示");

     while (OCI_FetchNext(rs))

    {

        printf("LogID %s/n", OCI_GetString(rs, 1));

        printf("module_name %s, operate %s /n", OCI_GetString(rs, 3),OCI_GetString(rs,4));

    }

    OCI_Cleanup();

    return EXIT_SUCCESS;

}

发布了38 篇原创文章 · 获赞 4 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/tomatozq/article/details/5943294