well this is my code i am getting the attr in String (attr is column name) now i want to set it to the object which i made in MedicineParam for example if attr contains dosage then the setter should also be set itself so dosage also i cant use
if(rs.getString("dosage") != null)
medici.setDosage(rs.getString("dosage"));
because if a field is not there then it will throw a sql exception
public ArrayList<MedicineParam> viewParticularDataOnClick(String medName,
String attr) {
ArrayList<MedicineParam> newList = new ArrayList<MedicineParam>();
try{
Class.forName(DRIVER);
String selectTableSQL = "SELECT "+attr+", companyname FROM
medicineinfo WHERE companyname != 'doecompany' AND medicinename = '"+medName+"'";
System.out.println(selectTableSQL);
Statement statement = this.checkconnection().createStatement();
ResultSet rs = statement.executeQuery(selectTableSQL);
final String s = "medici.set"+attr;
while (rs.next()) {
MedicineParam medici =new MedicineParam();
if(rs.getString("companyname") != null)
medici.setCompanyname(rs.getString("companyname"));
if(rs.getString(attr) != null)
medici.set...??.....(rs.getString(attr)); // THIS LINE how to set the setter w r t the name of attr
newList.add(medici);
}
rs.close();
statement.close();
this.checkconnection().close();
}
catch(Exception e){
}
return newList;
}
......
public class MedicineParam {
private long id;
private String companyname;
private String medicineName;
private String description;
private String dosage;
private String sideEffects;
private String warnings;
private String overdose;
private String interaction;
private String url;
//setter
//getter...
}
Any help?
If you can't create the getters and setters for some columns, you can add these columns into a specific or generic Map in the MedicineParam object.
MedicineParam additions:
private Map<String, String> attributes;
public void addAttribute(String attribute, String value) {
if (this.attributes == null) {
this.attributes = new HashMap<String, String>();
}
this.attributes.put(attribute, value);
}
public Map<String, String> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}
In your while loop, use this:
if(rs.getString(attr) != null) {
medici.addAttribute(attr, rs.getString(attr));
}
You can change the Map to be a Map<String, Object> to have a more generic map if you need to.
You can then get the values from the object:
medici.getAttributes().get("key");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With