• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PreparedStatement.execute(); Always Returns False

 
Greenhorn
Posts: 11
Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic