how to use an update statement in java web?

0

I'm trying to modify data in a table with the update statement in java web, use a servlet to capture the data and send it to a java class called " adoproducto " with a method called " Modify "that receives two parameters id (type integer) and descipcion (type varchar). my priblema is this:

When I write the sentence in this way

 Consulta="UPDATE producto SET descripcion = '"+descripcion+"' WHERE id ="+id;

simply the servlet does not run only load and load but never runs, then when I write the sentence this way

 Consulta="UPDATE producto SET descripcion = "+descripcion+" WHERE id ="+id;

If you execute the servlet but do not modify any record, in my opinion the correct form is the first note that in the first case in the description field I use single quotes because it is varchar type and the second case I do not, but if manages to execute the servlet.

public boolean  Modificar(String descripcion ,int id){
if (!conexion.estaconectado()){          
  conexion.conectar();
   }    
  String Consulta="";
  Consulta="UPDATE producto SET descripcion = '"+descripcion+"' WHERE id ="+id;

  int r=conexion.EnviarActualizacion(Consulta);
  if (r!=0){
  return true;
  }
  else{
  return false;
  }
}


public class conexion {
public static  Connection cnx = null;
public static String msg ="";

public static String conectar(){

try{   
    /*pro.agrega(entipro);*/
    Class.forName("oracle.jdbc.OracleDriver");
   cnx= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","SYSTEM","system");
  msg="conectado";
   } 
catch(SQLException e){
 msg = e.getMessage();
   }
catch(ClassNotFoundException e){
msg = e.getMessage();
   }
return msg;
}

public static boolean estaconectado(){

    if (cnx!=null){
    return true;
    }
    else{
     return false;
    }
}

public static int EnviarActualizacion(String cons){
   try {
      return ejecutarActualizacion(cnx.prepareStatement(cons));
       }
   catch (SQLException e){
      msg="Error al actualizar tabla : " + e.getMessage();
       }
     return 0;
       } 


public static int ejecutarActualizacion(PreparedStatement sp){

 try{
     return sp.executeUpdate();
 }catch(SQLException e){
     msg ="eror al actualizar la tabla"+ e.getMessage();
 }
 return 0;
}

}

String des = request.getParameter("descripcion");
int codigo = Integer.parseInt(request.getParameter("codigo"));
  lnegocio ln = new lnegocio();
  boolean estado = ln.modificar(des, codigo);           
  out.println(estado);
    
asked by Frank Campos Vilchez 12.06.2018 в 18:06
source

0 answers