请帮助!!!!!!! 在DB中,我有2行与此查询。
SELECT D.DEALER_CODE
FROM SCOTT.T_DEALERSHIP D,SCOTT.T_DEALER_BILLING DB
WHERE D.DEALER_CODE = DB.DEALER_CODE
AND DEALER_NAME LIKE 'XTIME%'
AND (RNR_CUST_NUM = '546' OR RNR_CUST_NUM = '43356'OR RNR_CUST_NUM = '7637055' OR RNR_CUST_NUM ='7637055' OR RNR_CUST_NUM IS NULL)
AND (RCI_STORE_NUMBER IS NULL OR RCI_STORE_NUMBER = '05')
AND (RCI_AREA_NUMBER = '01'OR RCI_AREA_NUMBER IS NULL)
AND (RCI_DEALER_NUMBER IS NULL AND DEALER_Address1 LIKE UPPER('1500 ORACLE%')
AND DEALER_CITY =UPPER('BAKERSFIELD') AND DEALER_ZIP LIKE'6%')
使用下面的代码,当DB中有2条记录时,我只得到1行。当数据库中只有一条记录时,它可以工作,但是当有更多记录时则不行。此外,rs.last()
方法也会被跳过并退出结果集。我必须评论它执行一个记录。我不想使用COUNT
。
ResultSet rs = stmt.executeQuery(sql1);
System.out.println("" + sql1);
while(rs.next()) {
rs.last();
int Rows = rs.getRow();
System.out.println("Rows are " + Rows);
}
答案 0 :(得分:0)
如果您只想计算数据库中的记录,那么您应该使用SELECT count(*) FROM ...
形式的查询。如果您确实需要按照上面列出的方式执行查询,那么您希望在while
循环之外设置一个计数器变量,如下所示:
int rowNum = 0;
while (rs.next()) {
rowNum++;
}
System.out.println("Number of records: "+rowNum);
答案 1 :(得分:0)
我强烈建议不要通过JDBC将所有数据都返回给Java,以确定行数。如果您只想知道与条件匹配的行数,请执行select count(*)查询:
在SQL中:
SELECT COUNT(*)
FROM SCOTT.T_DEALERSHIP D,SCOTT.T_DEALER_BILLING DB
WHERE D.DEALER_CODE = DB.DEALER_CODE
AND DEALER_NAME LIKE 'XTIME%'
AND (RNR_CUST_NUM = '546' OR RNR_CUST_NUM = '43356'OR RNR_CUST_NUM = '7637055' OR RNR_CUST_NUM ='7637055' OR RNR_CUST_NUM IS NULL)
AND (RCI_STORE_NUMBER IS NULL OR RCI_STORE_NUMBER = '05')
AND (RCI_AREA_NUMBER = '01'OR RCI_AREA_NUMBER IS NULL)
AND (RCI_DEALER_NUMBER IS NULL AND DEALER_Address1 LIKE UPPER('1500 ORACLE%')
AND DEALER_CITY =UPPER('BAKERSFIELD') AND DEALER_ZIP LIKE'6%')
然后在Java中:
ResultSet rs = stmt.executeQuery(sql1);
System.out.println("" + sql1);
int rows = 0;
if(rs.next()) {
rows = rs.getInt();
}
System.out.println("Rows are " + rows);
根据OP的评论进行编辑
除了计数之外,要从数据库中获取值,只需扫描值。
SQL:
SELECT D.DEALER_CODE
FROM SCOTT.T_DEALERSHIP D,SCOTT.T_DEALER_BILLING DB
WHERE D.DEALER_CODE = DB.DEALER_CODE
AND DEALER_NAME LIKE 'XTIME%'
AND (RNR_CUST_NUM = '546' OR RNR_CUST_NUM = '43356'OR RNR_CUST_NUM = '7637055' OR RNR_CUST_NUM ='7637055' OR RNR_CUST_NUM IS NULL)
AND (RCI_STORE_NUMBER IS NULL OR RCI_STORE_NUMBER = '05')
AND (RCI_AREA_NUMBER = '01'OR RCI_AREA_NUMBER IS NULL)
AND (RCI_DEALER_NUMBER IS NULL AND DEALER_Address1 LIKE UPPER('1500 ORACLE%')
AND DEALER_CITY =UPPER('BAKERSFIELD') AND DEALER_ZIP LIKE'6%')
爪哇:
ResultSet rs = stmt.executeQuery(sql1);
System.out.println("" + sql1);
List<String> rows = new ArrayList<String>();
while(rs.next()) {
rows.add(rs.getString());
}
System.out.println("There are " + rows.size() + " rows.");