Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Something like this:
Java
public void convert(Category cat)
{
//convert cat into string sql "insert into category() values()"  
 
}
Posted
Updated 16-Sep-10 10:17am
v2

Easy way to use "toString()" in your Category Class, and you don't need to create any method to convert Java Object into SQL String.
 
Share this answer
 
solved look this code create by myself

Java
Autos aut=new Autos();
       aut.setAuto("Auto1");
       aut.setColor("Negro");
       aut.setId_auto(12);
       aut.setPeso(10000);
       aut.setTama("Grande");

   Field[] fields = aut.getClass().getDeclaredFields();
   String campos="";
   String valores="";
   for (int i = 0; i < fields.length; i++) {
      try{

           String name = fields[i].getName();
           String value = fields[i].get(aut).toString();
           if(i!=0){
               campos=campos+",";
               valores=valores+",";
           }
           if(fields[i].get(aut) instanceof String){
               valores=valores+"'"+value+"'";
           }
           else{
               valores=valores+value;
            }
           campos=campos+name;
       } catch (IllegalAccessException ex) {}
   }
     String sql="insert into dbo."+aut.getClass().getSimpleName().toLowerCase()+"("+campos+")values("+valores+");";
     System.out.println(sql);
 
Share this answer
 
Comments
Adrabi Abderrahim 16-Sep-10 19:33pm    
Nice, by using reflection ^_^
Manuel Rodriguez Coria 16-Sep-10 19:43pm    
adrabi your solution generates the same?
if not you know a better way to do this?

cheers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900