This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

PreparedStatement.execute(); Always Returns False

 
Gehan Fernando
Greenhorn
Posts: 11
Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Experts

Happy to see you again. I am developing a method to call the data from the database according to business rule I cannot change the stored procedure because it given by CUSTOMER. My question is the following code is use to get data from the database

public class CustomerManager {

private DataSource dataSource;
private static final String SPROC_NAME = "{call FetchMarketInfoData(?,?,?,?,?,?,?,?,?,?)}";

public void setDataSource(DataSource source) {
this.dataSource = source;
}

public List<Customer> GetAll() throws SQLException {

Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(SPROC_NAME);

stmt.setString(1, null);
stmt.setDate(2, null);
stmt.setDate(3, null);
stmt.setString(4, null);
stmt.setString(5, null);
stmt.setString(6, null);
stmt.setBoolean(7, false);
stmt.setBoolean(8, false);
stmt.setBoolean(9, false);
stmt.setBoolean(10, false);

boolean hasResult = stmt.execute();

List<Customer> iList = new ArrayList<Customer>();

while (hasResult) {
ResultSet res = stmt.getResultSet();
while (res.next()) {
iList.add(CustomerMapper.mapRow(res));
}
hasResult = stmt.getMoreResults();
}
return iList;

}

}

But all the time hasResult shows “FALSE” I checked with SP for my testing purpose I added “SET NOCOUNT ON” after that I run the code, wonder I got the result as I expected then again I remove the “SET NOCOUNT ON” from the SP and I try the code, Gosh as usual hasResult shows “FALSE”, What’s the reason for it. Is anybody know the reason for it?
 
Dave Tolls
Ranch Hand
Posts: 2365
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API for PreparedStatement.execute():
"Returns - true if the first result is a ResultSet object; false if the first result is an update count or there is no result"

It doesn't mean there is no result set.
It means the procedure returns and update count as well as any results.

By default a SQL Server SP returns a count of rows affected.
 
Gehan Fernando
Greenhorn
Posts: 11
Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally I found the answer.

private DataSource dataSource;
private ResultSet res = null;

public List<Customer> GetAll() throws SQLException {

Connection conn = this.dataSource.getConnection();
PreparedStatement stmt = conn
.prepareStatement("{call GetAllCustomers}");

boolean results = stmt.execute();
int rowsAffected = 0;

while (results || rowsAffected != -1) {
if (results) {
res = stmt.getResultSet();
break;
} else {
rowsAffected = stmt.getUpdateCount();
}
results = stmt.getMoreResults();
}

List<Customer> customerList = new ArrayList<Customer>();

while (results) {
res = stmt.getResultSet();
while (res.next()) {
customerList.add(CustomerMapper.mapRow(res));
}
results = stmt.getMoreResults();
}
return customerList;
}
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!