Execute an SQL statement

This section describes how an SQL statement is executed.

 

What does this section cover?

Execute an SQL statement

The following method demonstrates how an SQL statement can be executed.

In the example below, the SQL statement has one parameter, the account ID, and retrieves all the bills of a specific account.

getBillsInformation
public ArrayList<String> getBillsInformation(String accountID) throws Exception{
 
 	ArrayList<String> bills = new ArrayList<String>();
 	
 	BigDecimal billToPaymentAmount = null;
 	Integer billDeleted = null;
 	String billNum = null;
 	Date billFromDate = null;
 
 	ArrayList<Object> parameters = new ArrayList<Object>();
 	String sql = "";
 	
	//Here we define the parameters of the SQL
 	parameters.add(accountID);
 
	//The SQL statement using binding variables 
 	sql = 
 	"\n SELECT BILLNUM,BILLDELETED,BILLFROMDATE,BILLTOPAYAMT " + 
 	"\n FROM BILLS " +
 	"\n WHERE BILLS.ACCRECID = ? " ;
 
	//At this point we execute the SQL
 	ResultSetUtil rsu = SQLUtil.executeUsingPreparedStatement(sql, getOrganisationID(), parameters, getCRMSession().getDbtype());

	if (rsu!=null && rsu.getRowCount()>0)
 	{
	
	//Traverse the ResultSetUtil object returned to obtain the results
	 while(rsu.next())
 		{
 
 		if(rsu.getObject("BILLNUM")!=null)
 			billNum = rsu.getString("BILLNUM");
 
 		if(rsu.getObject("BILLDELETED")!=null)
 			billDeleted = rsu.getInteger("BILLDELETED");
 
 		if(rsu.getObject("BILLFROMDATE")!=null)
 			billFromDate = rsu.getDate("BILLFROMDATE");
 
 		if(rsu.getObject("BILLTOPAYAMT")!=null)
 			billToPaymentAmount = rsu.getBigDecimal("BILLTOPAYAMT");
 
 		bills.add(billNum+"-"+billDeleted+"-"+billFromDate+"-"+billToPaymentAmount);
 		}
 	}

 	return bills;
 }