I have a table called accounts in which i am searching the data by account name, the search is working perfectly. Suppose if i am trying to search a which is not in my database, it shows an error, so i want to handle the exception by displaying an message that the data is not exist in the table. How can i dot it? I am using spring mvc and hibernate 3 with an back end mysql.
Here is my code.
This is my hibernate dao:
@SuppressWarnings("unchecked")
@Transactional(readOnly=true)
public List<Accounts> searchAccounts(String account){
System.out.println("Executing Search Accounts Query:::::::::::::::::::::::::::::::::::::::::::::::");
List<Accounts> accounts = getHibernateTemplate().find("from Accounts a here a.accountName = ?", account);
if (accounts != null)
{
return accounts;
}
return null;
}
This is my search Controller:
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
throws Exception{
String accountName = ServletRequestUtils.getStringParameter(request, "accountName");
List<Accounts> accounts = accountsDao.searchAccounts(accountName);
if (accounts == null || accounts.isEmpty()){
ModelAndView mav = new ModelAndView("forward:searchaccounts.htm");
System.out.println("searching data");
return mav.addObject("message", "message.form.searchaccounts");
} else {
ModelAndView mav = new ModelAndView();
return mav.addObject(accounts);
}
}
This is my search accounts jsp:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<div class="boxed">
<div class="title">Account Search</div>
<div class="content">
<form id="form4" method="get" action="#">
<fieldset>
<legend>Search</legend>
<input id="inputtext3" type="text" name="inputtext3" value="" class="textfield" />
<input id="inputsubmit2" type="submit" name="inputsubmit2" value="Search" class="button" />
<p class="tiny"><a href="#">Advanced Search</a></p>
</fieldset>
</form>
</div>
</div>
</div>
<div id="col-form">
<div class="boxed">
<div class="title">Group Account</div>
<div class="content">
<table border="1" width=100% >
<tr>
<td bgcolor="#BBDDFF" >Account Id</td>
<td bgcolor="#BBDDFF" >Account Group</td>
<td bgcolor="#BBDDFF" >Closing Balance</td>
<td bgcolor="#BBDDFF" >Account Type</td>
<td bgcolor="#BBDDFF" >Remarks</td>
</tr>
<c:forEach var="accounts" items="${accountsList}">
<tr bgcolor="#FFFFFF">
<td>${accounts.accountId}</td>
<td><a href="accountsledger.htm?accountId=${accounts.accountId}">${accounts.accountGroup}</a></td>
<td>${accounts.closingBalance}</td>
<td>${accounts.closingType}</td>
<td>${accounts.remarks}</td>
</tr>
</c:forEach>
</table>
</div> <!-- content -->
</div> <!-- boxed -->
</body>
</html>