Update a record "amount" JAVA

0

I am practicing making a beverage sales system, in which I have a table with the products, with a button to add to the cart. If the selected product is ALREADY in the cart, it only increases its AMOUNT by 1. Otherwise, add it to the list and place quantity 1.

The Purchase entity has a constructor with 3 parameters (Str product, int price, int quantity). and it operates with a separate table in the database called ESTA_COMPRA with the same fields in it.

With the code I have now, he is pulling me:

  

"net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc ::: 4.0.2 Incorrect Argument in a JDBC Call: parameter index out of range: 3       at net.ucanaccess.jdbc.UcanaccessPreparedStatement.setInt (UcanaccessPreparedStatement.java:536)       at sistemaventasbebidas.DAO.update_Cantidad (DAO.java:141)       at sistemaventasbebidas.VistaController.addCarro2 (VistaController.java:205) "

I do not know why I throw outofbond (3) if 3 is the number of the record Amount in the BD.

I have in the DAO:

/////////update cantidad en DB esta_compra///////////
public void update_Cantidad(entidad_Compras ec){
   DBCon conec = new DBCon();
   String sql= "UPDATE ESTA_COMPRA SET  cantidad=?" + "WHERE producto=?";
    PreparedStatement ps = null; 
    try {
            ps = conec.Connect().prepareStatement(sql);
            ps.setInt(3, ec.getCantidad());
            ps.executeUpdate();



        } catch (SQLException ex) {
            Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
        }

}

and in the controller:

@FXML
private void agregarCarro2(){

if (! tabla_bebidas.getSelectionModel().isEmpty()){
    entidad_Bebidas bebida = tabla_bebidas.getSelectionModel().getSelectedItem();    
String produ = tproducto.getText();
int precio = Integer.parseInt(tprecio.getText());      // seteo los 3 parametros para ingresarle al objeto EC
//cantidad = cantidad + 1;

//stock   aca faltaria una funcion para ir actualizando el stock en la bd, pero que pueda volver a la normalidad en caso de cancelar la compra.
if (Integer.parseInt(tstock.getText())> 0){
int stockinicial = bebida.getStock();
int stockactual = Integer.parseInt(tstock.getText());
int nuevoStock = stockactual - 1;
tstock.setText(String.valueOf(nuevoStock));
} else{
   lblStock.setVisible(true);  
}
////////

//precio
total =total +  precio;
tTotal.setText(String.valueOf(total));
//


DAO dao = new DAO();
entidad_Compras ec = new entidad_Compras(produ,precio,cantidad);
if (dao.esta_En_Carrito(produ)){
    ec.setCantidad(ec.getCantidad() + 1);
    dao.update_Cantidad(ec); //si esta en el carrito suma cantidad
    loadTablaCompras();
}else{
    //insertar compra en la bd
    ec.setCantidad(1);
    dao.Agregar_Producto_Carrito(ec);
    dao.update_Cantidad(ec);
    loadTablaCompras();
}
}//tablabebidas

}//agregar carrito 2

I'm not 100% sure of what I'm doing haha. I'm learning on my own! Greetings

    
asked by Gabriel Mc Gann 22.02.2018 в 19:57
source

1 answer

0

The error that throws you is because you have to indicate the number of the parameter, in this case it must go 1 and not 3 for quantity and additionally you have to specify another for product, I put setInt (as an example for product) but This will depend on the type of data of the product code in your case. It is necessary to specify these two parameters since your query requires it.

 ps = conec.Connect().prepareStatement(sql);
 ps.setInt(1, ec.getCantidad());   
 ps.setInt(2,datoproducto)
 ps.executeUpdate();

Your query requires two parameters, so the error

String sql= "UPDATE ESTA_COMPRA SET  cantidad=?" + "WHERE producto=?";

This is a good reference link

    
answered by 22.02.2018 / 20:17
source