Java and OCI are the two most commonly used programming languages for the development of Oracle databases, as well as for TimesTen.
It is slightly more complicated than PRO*C,OCI programming. But OCI provides finer-grained control, providing richer functionality and better performance. Therefore, for programmers who are familiar with the C language, for performance and control, and for easy debugging, the OCI should be preferred rather than pro*c.
Like Pro*c, OCI supports the use of TNS and Easy connect string (DSN) to connect TimesTen.
You can specify the appropriate interface by using connect string and Two_task.
Since most of the concepts have been introduced in the minimalist proc Connection TimesTen program, this is not explained here, but directly to the source code and makefile.
HELLOWORLD.C Program
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>#include <errno.h>#include "Tt_version.h"#include <oci.h>StaticText *crtable ="CREATE Table A (a int)";StaticText *insert ="INSERT into a values (12345)";Static Char*service ="tnssampledb_1122";Static Char*username ="Tthr";Static Char*password ="TimesTen";StaticOcienv *ENVHP;StaticOcierror *ERRHP;StaticOcisession *AUTHP = (ocisession *)0;StaticOciserver *SRVHP;StaticOcisvcctx *SVCHP;Static voidCheckerr (Ocierror * errhp, sword status);intMain (intargcChar*argv[]) {ocistmt *stmthp, *STMTHP1; Ocienvcreate ((ocienv * *) & ENVHP, (UB4) Oci_default, (dvoid *)0, (Dvoid * (*) (dvoid *, size_t))0, (Dvoid * (*) (dvoid *, dvoid *, size_t))0, (void(*) (Dvoid *, dvoid *))0, (size_t)0, (Dvoid *)0); Ocihandlealloc (dvoid *) ENVHP, (Dvoid *) & ERRHP, Oci_htype_error, (size_t)0, (Dvoid *)0); Ocihandlealloc (dvoid *) ENVHP, (Dvoid *) & SRVHP, Oci_htype_server, (size_t)0, (Dvoid *)0); Ocihandlealloc (dvoid *) ENVHP, (Dvoid *) & SVCHP, Oci_htype_svcctx, (size_t)0, (Dvoid *)0); Ociserverattach (SRVHP, ERRHP, (text *) service,strlen((Const Char*) service),0); Ociattrset ((dvoid *) SVCHP, Oci_htype_svcctx, (dvoid *) SRVHP, (UB4)0, Oci_attr_server, (Ocierror *) ERRHP); Ocihandlealloc ((dvoid *) ENVHP, (Dvoid *) & AUTHP, (UB4) oci_htype_session, (size_t)0, (Dvoid *)0); Ociattrset ((dvoid *) AUTHP, (UB4) oci_htype_session, (dvoid *) Username, (UB4)strlen((Char*) username), (UB4) oci_attr_username, ERRHP); Ociattrset ((dvoid *) AUTHP, (UB4) oci_htype_session, (dvoid *) password, (UB4)strlen((Char*) password), (UB4) Oci_attr_password, ERRHP); Checkerr (ERRHP, Ocisessionbegin (SVCHP, ERRHP, AUTHP, Oci_cred_rdbms, (UB4) Oci_defaul T)); Ociattrset ((dvoid *) SVCHP, (UB4) Oci_htype_svcctx, (dvoid *) AUTHP, (UB4)0, (UB4) oci_attr_session, ERRHP); Checkerr (ERRHP, Ocihandlealloc (dvoid *) ENVHP, (Dvoid *) & STMTHP, oci_htype_stm T, (size_t)0, (Dvoid *)0)); Checkerr (ERRHP, Ocihandlealloc (dvoid *) ENVHP, (Dvoid *) & STMTHP1, Oci_htype_st MT, (size_t)0, (Dvoid *)0)); Checkerr (ERRHP, Ocistmtprepare (STMTHP, ERRHP, Crtable, (UB4)strlen((Char*) crtable), (UB4) Oci_ntv_syntax, (UB4) oci_default)); Checkerr (ERRHP, Ocistmtexecute (SVCHP, STMTHP, ERRHP,1,0,0,0, Oci_default)); Checkerr (ERRHP, Ocitranscommit (SVCHP, ERRHP,0)); Checkerr (ERRHP, Ocihandlefree (dvoid *) STMTHP, oci_htype_stmt)); Checkerr (ERRHP, Ocistmtprepare (STMTHP1, ERRHP, insert, (UB4)strlen((Char*) insert), (UB4) Oci_ntv_syntax, (UB4) oci_default)); Checkerr (ERRHP, Ocistmtexecute (SVCHP, STMTHP1, ERRHP,1,0,0,0, Oci_default)); Checkerr (ERRHP, Ocitranscommit (SVCHP, ERRHP,0)); Checkerr (ERRHP, Ocihandlefree (dvoid *) STMTHP1, oci_htype_stmt)); Checkerr (ERRHP, Ocisessionend (SVCHP, ERRHP, AUTHP, Oci_default)); Checkerr (ERRHP, Ociserverdetach (SRVHP, ERRHP, Oci_default)); Checkerr (ERRHP, Ocihandlefree (dvoid *) ENVHP, oci_htype_env)); Ocihandlefree ((dvoid *) SRVHP, oci_htype_error); Ocihandlefree ((dvoid *) SRVHP, oci_htype_env);}Static voidCheckerr (Ocierror * errhp, sword status) {text errbuf[ +]; SB4 Errcode =0;Switch(status) { CaseOci_success: Break; CaseOci_success_with_info: (void)printf("error-oci_success_with_info\n"); Break; CaseOci_need_data: (void)printf("error-oci_need_data\n"); Break; CaseOci_no_data: (void)printf("error-oci_nodata\n"); Break; CaseOci_error: (void) Ocierrorget ((dvoid *) ERRHP, (UB4)1, (text *) NULL, &errcode, Errbuf, (UB4)sizeof(ERRBUF), oci_htype_error); (void)printf("Error-%.*s\n", +, (Char*) errbuf); Break; CaseOci_invalid_handle: (void)printf("error-oci_invalid_handle\n"); Break; CaseOci_still_executing: (void)printf("error-oci_still_execute\n"); Break; CaseOci_continue: (void)printf("error-oci_continue\n"); Break;default: Break; }}
Makefile
## Build TimesTen Sample Programs (Unix)# Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.## $Revision#Tt_mvl_linux=0Instdir=/home/oracle/timesten/tt1122Commdir=$(Instdir)/quickstart/sample_code/commonTtoracle_home=$(Instdir)/ttoracle_home/instantclient_11_2Ociincs=$(Ttoracle_home)/sdk/includeC= gccPlatcflags= -Os-finline-functionsLdflags=INCS= -I$(Ociincs) -I$(Instdir)/include-I$(Commdir)CFLAGS=$(Platcflags)$(INCS)-GTtlink= -L$(Instdir)/lib-L$(Ttoracle_home) -Wl,-rpath,$(Instdir)/lib,-rpath,$(Ttoracle_home) Ifeq ($(Tt_mvl_linux),1)Extralibs=-ldl-lnsl-lstdc++endifIclibs=$(Ttlink)-LC-LCLNTSH-LPTHREAD-LM-LRT$(Extralibs)HELLOWORLD= HelloWorldHelloworldobjs= HELLOWORLD.OProgs=$(HELLOWORLD)$(TPTBM)$(Plsqloci)## Top level targets#All : $(Progs)Clean :Rm-f$(Progs) *.O## Targets for building executables#$(HELLOWORLD): $(Helloworldobjs)$(CC)-O[email protected] $(Ldflags)$(Helloworldobjs)$(Iclibs)## Target for building object files#. C.o: $(CC)$(CFLAGS)-O[email protected]-C$<
Summarize
You can see that the OCI code is significantly longer than Pro*c and Java, but this is not a problem because it is a fixed pattern that you can encapsulate into functions or make dynamic and static link libraries.
It is important that we need the performance and flexible control provided by OCI.
On the comparison of Pro*c and OCI, we suggest that we look at Wang Yong's written Linux operating system pro*c and OCI design analysis and comparison.
Minimalist OCI Connection TimesTen program