Re: Shorten setString lines
On Nov 13, 10:03 pm, RedGrittyBrick <RedGrittyBr...@SpamWeary.foo>
wrote:
franca...@yahoo.com wrote:
I have a PreparedStatement with alot of values to insert into an
Oracle Database.
Anyway to shorten the setString lines in a loop or any other way to
shorten this??
//Database connection part here
//......
private PreparedStatement stmt;
public void houseMethod(BeanInfo theObject)
{
stmt = connection.prepareStatement("Insert into MainTable
(house, zipcode, city, county, phone, mortgage, tax, insurance, state,
land) values (?,?,?,?,?,?,?,?,?,?)");
stmt.setString(1, theObject.getHouse());
stmt.setString(2, theObject.getZipcode());
stmt.setString(3, theObject.getCity());
stmt.setString(4, theObject.getCounty());
stmt.setString(5, theObject.getPhone());
stmt.setString(6, theObject.getMortgage());
stmt.setString(7, theObject.getTax());
stmt.setString(8, theObject.getInsurance());
stmt.setString(9, theObject.getState());
stmt.setString(10, theObject.getLand());
stmt.executeUpdate();
}
I suppose if you implemented a `String getIndexedString(int index)` in
theObject's class, you could use a for loop.
It feels a bit brittle to me. I'm sure this idea deserves to be shot
down in flames :-)
Well, let's put it this way:
If we applied the "Occam's Razor" principle and consulted
the person responsible for maintaining that code, I think
that the original piece of code would be a clear winner.
Cleverness can sometimes cause a maintainability nightmare.
I think the first approach is quite clear and concise, though
I would reconsider the names of the identifiers :-)
Hmmm,
String[] columnNames = { "house", "zipcode" ... };
String sql = "insert into MainTable (" + makeList(coumnNames) + ...
for (int i = 1; i <= columnNames.length; i++) {
stmt.setString(i, theObject.getNamedString(columnNames[i-1]));
}
I'll get my coat.
Hey, it's the thought that counts :-)
--
Chris