问题
I'm having trouble with the getFetchSize()
function.
I simply need to know if the SQL query has returned zero rows.
I've tried this simple statement:
if (rs.getFetchSize()==0)
System.out.println("HEADLINE");
where rs
is of the ResultSet
type. The above code doesn't seem to work. It always prints the message whether rs
is empty or not.
I checked the SQL query itself and it correctly returned non-empty result when rows existed.
Any thoughts on how I can determine whether the query has returned 0 rows? I googled it and couldn't find any answer.
回答1:
ResultSet.getFetchSize() doesn't return the number of results! From here:
Standard JDBC also enables you to specify the number of rows fetched with each database round-trip for a query, and this number is referred to as the fetch size
You can iterate across the result set and if you don't have any entries, then you can determine that you got no results. Why can't you get the result size immediately ? Because the database is returning you a pointer to its results and it's relying on you to iterate through that (going back to the server for each row).
回答2:
The fetch size is the number of rows that should be retrieved from the database in each roundtrip. It has nothing to do with the number of rows returned.
What you should do is call rs.next() - iff your query didn't return any rows it will return false.
回答3:
Actually, I think another way to do what you are looking for is this:
Class.forName("org.exempleDatabase.Driver");
Connection conn = DriverManager.getConnection("jdbc:exempleDatabase:/../smartytwiti", "username", "password");
ResultSet resset = conn.createStatement().executeQuery("select count(*) from yourTable;");
resset.last();
int resnum = resset.getRow();
if(resnum<1)System.out.println("HEADLINE");
That will take the ResultSet, move to the last row of the set, and then give the number of that row in resnum. Just remember to move back with resset.beforeFirst() if you are going to iterate through the results with resset.next().
Hope that helps.
回答4:
On SQL Server You can put it into sql query:
COUNT(*) OVER(PARTITION BY 1)
like here:
select COUNT(*) OVER(PARTITION BY 1) as NUM,ident as ID,employee as EMP from invoice where date between '2017-01-01 00:00:00.000' and '2017-01-31 00:00:00.000'
And then, you get results like these:
NUM, ID, EMP
4, 28912, JOHN
4, 28913, JOHN
4, 28914, ADAM
4, 28915, PETER
And now, before processing all the data, you can read only first col and first cell to get number of records. Object array [invoices] to store results is declared only once, while getting thirst result.
Object[][] invoices = null;
Boolean b_inv = false;
[...]
try {
Statement stmt = db_conn.createStatement();
boolean results = stmt.execute(sql_query);
if (results) {
try (ResultSet rs1 = stmt.getResultSet()) {
Integer l = 0;
while (rs1.next()) {
if (!b_inv) {
invoices = new Object[rs1.getInt(1)][2];
b_inv = true;
}
invoices[l][0] = rs1.getInt(1);
invoices[l][1] = rs1.getString(2);
l++;
}
}
stmt.close();
}
} catch (SQLException | NumberFormatException dbsi1) {}
Isn't it good to put counting of records on SQL Server side?
回答5:
You can do what you're looking for using this code:
Class.forName("org.exempleDatabase.Driver");
Connection conn = DriverManager.getConnection("jdbc:exempleDatabase:/../smartytwiti", "username", "password");
ResultSet resset = conn.createStatement().executeQuery("select count(*) from yourTable;");
resset.next();//set the pointer to the first row(important!)
if(resset.getInt(1)==0)
System.out.println("HEADLINE");
回答6:
As mentioned earlier the getFetchSize does not get the number of results..
You can do something like this. Initialise an integer and increment it..
int count = 0
while(rs.next()) {
count++;
}
来源:https://stackoverflow.com/questions/14276249/resultset-getfetchsize-doesnt-seem-to-work