java count rows_獲取Java resultset中的行數

Does anyone know a better way of getting the number of rows in a Java resultset returned from a MySQL database? The resultset returned is not going to be the total number of rows read from the database so I don't think I can use SQL's COUNT aggregate function.

有沒有人知道更好的方法來獲取從MySQL數據庫返回的Java resultset中的行數?返回的resultset不會是從數據庫讀取的行總數,因此我不認為我可以使用SQL的COUNT聚合函數。

public static int getResultSetRowCount(ResultSet resultSet) {

int size = 0;

try {

resultSet.last();

size = resultSet.getRow();

resultSet.beforeFirst();

}

catch(Exception ex) {

return 0;

}

return size;

}

7 个解决方案

#1

18

A better answer is to forget about the number of rows until you've successfully loaded the ResultSet into an object or collection. You can keep the count of the number of rows with either of those options.

更好的答案是,在成功地將ResultSet加載到對象或集合之前,先忘掉行數。您可以使用其中任何一個選項來保持行數的計數。

It's important to close ResultSets (and all SQL resources like Connection and Statement) in the narrowest method scope possible. That means not passing ResultSet out of the persistence layer. Better to get the number of rows using a Collection size() call.

在盡可能窄的方法范圍內關閉結果集(以及所有SQL資源,如連接和語句)是很重要的。這意味着不從持久性層中傳遞ResultSet。最好使用集合size()調用獲取行數。

Stop thinking about databases and start thinking in terms of objects. Java's an object-oriented language.

停止思考數據庫,開始從對象的角度思考。Java是一個面向對象的語言。

#2

9

You can execute

你可以執行

SELECT FOUND_ROWS()

immediately after executing your SELECT statement to find the row count.

在執行SELECT語句之后立即查找行計數。

#3

2

You can always use SELECT COUNT() with the same exact conditions, before making the actual SELECT.

在進行實際的選擇之前,您總是可以使用具有相同條件的SELECT COUNT()。

#4

2

Here is my solution to this question (since I mostly want the number of records returned to build an array or something similar): Use a collection such as Vector instead.

這里是我對這個問題的解決方案(因為我主要想要返回的記錄的數量來構建一個數組或類似的東西):使用一個集合,比如Vector

public Vector getRecords(){

Vector records = new Vector();

init_conn_and_stmt_and_rs();

try {

rs = stmt.executeQuery("SELECT * FROM `table` WHERE `something` = 0");

while(rs.next()){

// Load the Vector here.

}

} catch (SQLException e) {

e.printStackTrace();

}finally{

close_rs_and_stmt_and_conn();

}

return records;

}

Clean and simple, no? Works for any size record set returned (no need to know the size before hand) and makes all the List methods available.

干凈,簡單,不是嗎?適用於返回的任何大小記錄集(不需要事先知道大小),並使所有列表方法可用。

This has served me well for a time now, but if someone sees a flaw in this, please let me know. Always want to make my practices better, ya know.

這對我來說已經有一段時間了,但是如果有人發現其中的缺陷,請告訴我。你知道,我總是想把我的做法做得更好。

#5

1

If you are using Java 6 you can use the JDBC4ResultSet class which has the getUpdateCount method that returns the number of the lines affected by a SQL Statement even for a Select Statement.

如果您正在使用Java 6,您可以使用JDBC4ResultSet類,該類具有getUpdateCount方法,該方法返回受SQL語句影響的行數,甚至對於Select語句也是如此。

See below the example code:

參見下面的示例代碼:

PreparedStatement ps = con.prepareStatement("select * from any_table ...");

JDBC4ResultSet rs = (JDBC4ResultSet)ps.executeQuery();

int rowNumber = rs.getUpdateCount();

I hope that this help!

我希望這能有所幫助!

#6

1

You can also use the following way to get the total records in the resultSet:

您還可以使用以下方法獲取resultSet中的總記錄:

statement = connect.createStatement();

resultSet = statement.executeQuery(sqlStatement);

resultSet.last();

int count = resultSet.getRow();

System.out.println(count);

count is the total returned rows for your result set. ;)

count是您的結果集返回的總數。

#7

0

See below the example code with the solution of Lalith :

參見下面的示例代碼,使用Lalith的解決方案:

public static int getResultSetRowCount(connection) {

int size = 0;

statement = connection.createStatement();

try {

resultSet = statement.executeQuery("SELECT FOUND_ROWS()")

resultSet.next()

size = resultSet.getInt(1)

}

catch(Exception ex) {

return 0;

}

statement.close();

return size;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值