java odbc timesten_timesten ODBC编程示例代码

//The following example constructs SQL statements within the application. The exampl//comments include equivalent embedded SQL calls for illustrative purposes.#include#include#include#includeusingnamespacestd;

#ifndef NULL#defineNULL 0#endif#defineMAX_NAME_LEN 50#defineMAX_STMT_LEN 100#defineMAX_MSG_LNG 512classTTODBCTest

{private:

HENV   henv;

HDBC   hdbc;

HSTMT  hstmt;

SDWORD  id;charname[MAX_NAME_LEN+1];charcreate[MAX_STMT_LEN];charinsert[MAX_STMT_LEN];charselect[MAX_STMT_LEN];

SQLLEN  namelen;

RETCODE rc;

SQLCHAR szErrorMsg[MAX_MSG_LNG];

SQLSMALLINT pcbErrorMsg;

SQLCHAR szSqlState[MAX_MSG_LNG];

SQLINTEGER pfNativeError;public:intprint_err(   HSTMT stmt );intTTODBCTest::example1(constchar*server,constchar*uid,constchar*pwd);

};intTTODBCTest::print_err( HSTMT stmt )

{

SQLError(this->henv,this->hdbc, stmt, szSqlState,&pfNativeError, szErrorMsg, MAX_MSG_LNG,&pcbErrorMsg );

cout<

}intTTODBCTest::example1(constchar*server,constchar*uid,constchar*pwd)

{/*EXEC SQL CONNECT TO :server USER :uid USING :pwd;*//*Allocate an environment handle.*//*Allocate a connection handle.*//*Connect to a data source.*//*Allocate a statement handle.*/SQLAllocEnv(&henv);

SQLAllocConnect(henv,&hdbc);

rc=SQLConnect( hdbc, (SQLCHAR*)server, SQL_NTS, (SQLCHAR*)uid, SQL_NTS, (SQLCHAR*)pwd, SQL_NTS);if(rc!=SQL_SUCCESS&&rc!=SQL_SUCCESS_WITH_INFO)return(print_err( SQL_NULL_HSTMT));

SQLAllocStmt(hdbc,&hstmt);/*EXEC SQL CREATE TABLE NAMEID (ID integer, NAME varchar(50));*//*Execute the SQL statement.*/strcpy( (char*)create,"CREATE TABLE NAMEID (ID INTEGER, NAME VARCHAR(50))");

rc=SQLExecDirect(hstmt, (SQLCHAR*)create, SQL_NTS);if(rc!=SQL_SUCCESS&&rc!=SQL_SUCCESS_WITH_INFO)return(print_err(  hstmt));/*EXEC SQL COMMIT WORK;*//*Commit the table creation.*//*Note that the default transaction mode for drivers that support*//*SQLSetConnectOption is auto-commit and SQLTransact has no effect

SQLTransact(hdbc, SQL_COMMIT);

/* EXEC SQL INSERT INTO NAMEID VALUES ( :id, :name );*//*Show the use of the SQLPrepare/SQLExecute method:*//*Prepare the insertion and bind parameters.*//*Assign parameter values.*//*Execute the insertion.*/strcpy( (char*)insert,"INSERT INTO NAMEID VALUES (?, ?)");if(SQLPrepare(hstmt, (SQLCHAR*)insert, SQL_NTS)!=SQL_SUCCESS)return(print_err( hstmt));

SQLBindParameter(hstmt,1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER,0,0,&id,0, NULL);

SQLBindParameter(hstmt,2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, MAX_NAME_LEN,0, name,0, NULL);

id=500;

strcpy( (char*)name,"Babbage");if(SQLExecute(hstmt)!=SQL_SUCCESS)return(print_err(  hstmt));/*EXEC SQL COMMIT WORK;*//*Commit the insertion.*/SQLTransact(hdbc, SQL_COMMIT, SQL_COMMIT );/*EXEC SQL DECLARE c1 CURSOR FOR SELECT ID, NAME FROM NAMEID; *

/* EXEC SQL OPEN c1;*//*Show the use of the SQLExecDirect method.*//*Execute the selection.*//*Note that the application does not declare a cursor.*/strcpy(  (char*)select,"SELECT ID, NAME FROM NAMEID");if(SQLExecDirect(hstmt, (SQLCHAR*)select, SQL_NTS)!=SQL_SUCCESS)return(print_err( hstmt));/*EXEC SQL FETCH c1 INTO :id, :name;*//*Bind the columns of the result set with SQLBindCol.*//*Fetch the first row.*/SQLBindCol(hstmt,1, SQL_C_SLONG,&id,0, NULL);

SQLBindCol(hstmt,2, SQL_C_CHAR, name, (SQLLEN)sizeof(name),&namelen);

SQLFetch(hstmt);/*EXEC SQL COMMIT WORK;*//*Commit the transaction.*/SQLTransact(hdbc, SQL_COMMIT, SQL_COMMIT );/*EXEC SQL CLOSE c1;*//*Free the statement handle.*/SQLFreeStmt(hstmt, SQL_DROP);/*EXEC SQL DISCONNECT;*//*Disconnect from the data source.*//*Free the connection handle.*//*Free the environment handle.*/SQLDisconnect(hdbc);

SQLFreeConnect(hdbc);

SQLFreeEnv(henv);return(0);

}intmain()

{

TTODBCTest tt;

tt.example1("ttocs1","unitele","lemontea");return0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值