I have a JTable
where I load the data that I have in my BD, based on those records I make a simple operation, the data I enter and with which I do the operation I declared them as Double
, at the moment to save the new data without performing the operation I get the following error, likewise if I do the operation does not register me and the same error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String can not be cast to java.lang.Double
private void registrarInventarioActionPerformed(java.awt.event.ActionEvent evt) {
try {
int rows = jTable1.getRowCount();
System.out.println(rows);
Class.forName("com.mysql.jdbc.Driver");
String query = ("INSERT INTO ajuste_inventario_tmp(N_Ajuste,Categoria,ID,Referencia,Producto,UM,P_Compra,P_Venta,Teorico,Fisico,Diferencia,Nuevo_Inv) values(?,?,?,?,?,?,?,?,?,?,?,?)");
PreparedStatement sentencia = conexion.prepareStatement(query);
for(int row = 0; row<rows ; row++)
{
variables.N_Ajuste = (String) jTable1.getValueAt(row, 0);
variables.Categoria = (String) jTable1.getValueAt(row, 1);
variables.ID = (String) jTable1.getValueAt(row, 2);
variables.Referencia = (String) jTable1.getValueAt(row, 3);
variables.Producto = (String) jTable1.getValueAt(row, 4);
variables.UM = (String) jTable1.getValueAt(row, 5);
variables.P_Compra = (Double) jTable1.getValueAt(row, 6);
variables.P_Venta = (Double) jTable1.getValueAt(row, 7);
variables.Teorico = (Double) jTable1.getValueAt(row, 8);
variables.Fisico = (Double) jTable1.getValueAt(row, 9);
variables.Diferencia = (Double) jTable1.getValueAt(row, 10);
String Nuevo_Inv = (String) jTable1.getValueAt(row, 11);
sentencia.setString(1, variables.N_Ajuste);
sentencia.setString(2, variables.Categoria);
sentencia.setString(3, variables.ID);
sentencia.setString(4, variables.Referencia);
sentencia.setString(5, variables.Producto);
sentencia.setString(6, variables.UM);
sentencia.setDouble(7, variables.P_Compra);
sentencia.setDouble(8, variables.P_Venta);
sentencia.setDouble(9, variables.Teorico);
sentencia.setDouble(10, variables.Fisico);
sentencia.setDouble(11, variables.Diferencia);
sentencia.setString(12, Nuevo_Inv);
sentencia.executeUpdate();
}
JOptionPane.showMessageDialog(null, "Inventario registrado");
} catch (HeadlessException | ClassNotFoundException | SQLException e) {
JOptionPane.showMessageDialog(null, e);
JOptionPane.showMessageDialog(null, "Error de conexión" + "\n" + e);
}
}
It can be seen that I bring the variables of another class, these are already declared respectively.
Here I do the operation:
double Fisico = Double.parseDouble(String.valueOf(tm.getValueAt(jTable1.getSelectedRow(),9)));
double Teorico = Double.parseDouble(String.valueOf(tm.getValueAt(jTable1.getSelectedRow(),8)));
if (Fisico == 0) {
JOptionPane.showMessageDialog(null, "No hubo cambios en la columna Fisico.");
}else{
double Diferencia = (Teorico - Fisico);
if (Diferencia < 0) {
Diferencia = (Diferencia * (-1));
}
tm.setValueAt(Diferencia, jTable1.getSelectedRow(), 10);
jTable1.clearSelection();
}