java - Is there any generic based alternatives for setting parameters in PrepareStatement -


i developing database application. using java.sql combined h2 embedded database. develop don't repeat yourself way.
set reuseable database row class , database property class follows: public class databaseproperty { private string propertyname; private t value; private boolean identifier;

public databaseproperty(string propertyname, t value, boolean identifier) {     this.propertyname = propertyname;     this.value = value;     this.identifier = identifier; }  public string getpropertyname() {     return propertyname; } public t getvalue() {     return value; } public void setvalue(t value) {     this.value = value; } public boolean isidentifier() {     return identifier; }  } 

and... public class databaserow { protected connection dbconnection; protected string tablename; protected hashset = new hashset<>();

    public databaserow() //with above variables. apologies being lazy type ;)     //here's problem part     //i'm trying automatically generate sql statement     //to check row specified primary unique keys (ex:- username , password combination log in)     public boolean existsintable(){     try {         string sql = "select * "+tablename+" ";          boolean addand = false;         for(databaseproperty d:columns) {             if(d.isidentifier()) {                 sql+=(addand?"and ":"")+d.getpropertyname()+" = ? ";                 addand = true;             }         }         preparedstatement ps = getdbconnection().preparestatement(sql); 

and code goes on...
problem not have generic based methods setting parameters in peparedstatement class. instead there setstring(int index,string s), etc.. please me overcome this.. there object oriented wrappers available, notorm php? there trade off between performance , coding ease such options?

try use this:

ps.setobject(index, object); 

it should work in cases index not null. think not problem case. if object null, need set type

ps.setobject(index, null, type); 

the type can parameter metadata object:

parametermetadata meta=ps.getparametermetadata(); int type = meta.getparametertype(index); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -