create table temp_test(iid_number number, name_varchar2 varchar2(32),
num_53 number(5,3), char_5 char(5), long_type long, raw_20 raw(20),
blob_type blob, integer_type integer , stat_time_date date);
Environment *env;
Connection *conn;ResultSet *rs;
env = Environment::createEnvironment();//创建环境变量
conn = env->createConnection(username,password,connstring); //创建数据库连接对象
stmt = conn->createStatement();//创Statement对象
string sql = "select * from temp_test";
stmt->setSQL(sql);//准备SQL语句
// 逐行获取表字段
try
{
rs = stmt->executeQuery();//执行SQL语句,返回结果集
while (rs->next()) //取数据
{
cout<<rs->getString(1)<<""<<rs->getString(2)<<" "<<rs->getInt(3)<<endl;
}
}
catch(SQLException &ex) //异常处理
{
cout << " Error Number "<< ex.getErrorCode() << endl;
cout << ex.getMessage() << endl;
}
///获取表字段属性temp_test为表名
MetaData custtab_metaData = conn->getMetaData("temp_test", MetaData::PTYPE_TABLE);
vector <MetaData> listOfColumns = custtab_metaData.getVector(MetaData::ATTR_LIST_COLUMNS);
unsigned int uiColumnNum = listOfColumns.size();
printf( "字段数=%u\n ", uiColumnNum);
for (int i=0; i <uiColumnNum; i++)
{
MetaData columnObj = listOfColumns[i];
//cout<<"columnObj.getType():"<<columnObj.getType()<<endl;
printf( "%d 列名:%s\n ", i+1, columnObj.getString(MetaData::ATTR_NAME).c_str());
printf( "%d 类型:%d,\n ", i+1, columnObj.getInt(MetaData::ATTR_DATA_TYPE)/*, getTypeString(columnObj.getInt(MetaData::ATTR_DATA_TYPE)).c_str()*/);
printf( "%d 大小:%d\n ", i+1, columnObj.getInt(MetaData::ATTR_DATA_SIZE));
printf( "%d 精度:%d\n ", i+1, columnObj.getInt(MetaData::ATTR_PRECISION));
printf( "%d 数值范围:%d\n ", i+1, columnObj.getInt(MetaData::ATTR_SCALE));
if( columnObj.getBoolean(MetaData::ATTR_IS_NULL) )
printf( "%d 允许为空\n ", i+1);
else
printf( "%d 不允许为空\n ", i+1);
int nType = columnObj.getInt(MetaData::ATTR_DATA_TYPE);
switch (nType)
{
case SQLT_DAT:
cout<<columnObj.getString(MetaData::ATTR_NAME).c_str()<<":is date\n";
break;
case SQLT_NUM:
cout<<columnObj.getString(MetaData::ATTR_NAME).c_str()<<":is number\n";
break;
case SQLT_CHR:
cout<<columnObj.getString(MetaData::ATTR_NAME).c_str()<<":is varchar2\n";
break;
case SQLT_STR:
cout<<columnObj.getString(MetaData::ATTR_NAME).c_str()<<":is SQLT_STR\n";
break;
case SQLT_LNG:
cout<<columnObj.getString(MetaData::ATTR_NAME).c_str()<<":is SQLT_LNG\n";
break;
case SQLT_BLOB:
cout<<columnObj.getString(MetaData::ATTR_NAME).c_str()<<":is SQLT_BLOB\n";
break;
case SQLT_INT:
cout<<columnObj.getString(MetaData::ATTR_NAME).c_str()<<":is SQLT_INT\n";
break;
default:
break;
}
}
//通过结果集获取字段属性
/* Create a statement and associate it with a select clause */
string sqlStmt="temp_test";
Statement *stmt=conn->createStatement(sqlStmt);
/* Execute the statement to obtain a ResultSet */
ResultSet *rset=stmt->executeQuery();
/* Obtain the metadata about the select list */
vector<MetaData>cmd=rset->getColumnListMetaData();
/* The metadata is a column list and each element is a column metaData */
int dataType=cmd[i].getInt(MetaData::ATTR_DATA_TYPE);
conn->terminateStatement(stmt);//终止Statement对象
env->terminateConnection(conn);//断开数据库连接
Environment::terminateEnvironment(env);// 终止环境变量