struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sean Burlington <s...@uncertainty.org.uk>
Subject is there a better way of creating SQL queries from form objects ?
Date Mon, 09 May 2005 14:37:47 GMT
Hi all,
	this is such a common problem that I'm sure there must be loads of 
advice on how to deal with it - I just can't find it.

I have a database containing many records

Searches can be performed on several text fields - and refined by 
boolean fields.

The html form thus consist of several text inputs and several checkboxes.

Only one text box should ever be filled in.

I can work out the resultant sql query by using lots of if statements as 
below but it just doesn't seem very elegant ...

I could also break the search down into several forms and so different 
actions - but this seems overly complex.

String query = "select order from po where";
String searchTerm = "";

if (!"".equals(search.getCode())){
     query += " code=?";
     searchTerm = search.getCode();
} else if (!"".equals(search.getParish())){
     query += " parish=?";
     searchTerm = search.getParish();
} else if (!"".equals(search.getDetails())){
     query += " details=?";
     searchTerm = search.getDetails();
}

if (search.isActiveMods()){
     query += " and active";
}
if (search.isActiveReclass()){
     query += " and reclass";
}

PreparedStatement statement = conn.prepareStatement(query);
statement.setString(1, searchTerm);

-- 

Sean

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message