我使用以下查询来获取列数。
select count(*) from all_tab_columns where owner='IULPROJECT' and table_name='SUPPLIERS';
此查询在Oracle命令行中有效,我得到了数字5,但如何在Java中返回此数字?
在Java中:
stmt=conn.createStatement();
query="select count(*) from all_tab_columns where owner='IULPROJECT' and table_name='SUPPLIERS' ";
rset=stmt.executeQuery(query);
System.out.println(stmt); //<-- what should I put here to get back my number "5" ?
答案 0 :(得分:3)
您的数据已包含ResultSet
,其中包含一个column
,因此您必须先调用next()
,因为ResultSet
Cursor
类型在第一个之前隐式定位。所以请调用next()
方法,然后使用count = rset.getInt(1)
调用您的数据。
rset=stmt.executeQuery(query);
int count = 0;
while (rset.next()) {
count = rset.getInt(1); // numbering of columns starts from 1 not from 0
}
System.out.println(count);
希望它对你有所帮助。 关心男人
答案 1 :(得分:1)
rset=stmt.executeQuery(query);
while (rset.next()) {
System.out.println(rset.getInt(1));
}
答案 2 :(得分:1)
您的查询将返回一个包含一行和一列的表,因为 count(*)是一个组函数。所以在获取ResultSet后,使用 next()转到第一行,然后使用 getInt()
获取值stmt=conn.createStatement();
query="select count(*) from all_tab_columns where owner='IULPROJECT' and table_name='SUPPLIERS' ";
rset=stmt.executeQuery(query);
rset.next();
System.out.println(rset.getInt(1));
答案 3 :(得分:1)
使用rset.getInt(1)。它将返回第一列的值。
while(rset.next){ System.out.println(rset.getInt(1));}
答案 4 :(得分:0)
就像背景信息一样,请注意
select count(*) from ...
总是会返回一行大于或等于零且永远不会为空的行(后者对所有人都是真的:
count(*)
呼叫)。
我最初把它作为评论,但* =斜体,我找不到逃避它的方法