In the following code I try to make 2 buttons from Arduino send only 1 or 2 and when it reaches java if I press 1 with the help of MySQL it will delete only one number and if I press 2 it will add more products. My doubt is that I can not find where the error is generating me
import com.panamahitek.PanamaHitek_Arduino;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
public class Mini_Proyecto_Principiante extends javax.swing.JFrame {
PanamaHitek_Arduino Arduino = new PanamaHitek_Arduino();
SerialPortEventListener listener;
ConexionMySQL mysql = new ConexionMySQL();
Connection cn = mysql.conectar();
String cantidad;
String sSQL, gua, ssSQL,sssSQL;
String SQL = " ";
String mensaje = " ";
/**
* Creates new form Mini_Proyecto_Principiante
*/
public Mini_Proyecto_Principiante() {
this.listener = new SerialPortEventListener() {
@Override
public void serialEvent(SerialPortEvent spe) {
try {
if (Arduino.isMessageAvailable()) {
String val;
val = Arduino.printMessage();
//msj.setText(val);
try {
sSQL = "SELECT cantidad FROM tbl_soda WHERE cantidad=10 ";
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery(sSQL);
while(rs.next()){
rs.getString("cantidad");
}
if ("1".equals(val)) {
cantidad = rs.getString("cantidad");
ssSQL = "DELETE FROM tbl_soda WHERE cantidad=" + sSQL;
PreparedStatement pst = cn.prepareStatement(ssSQL);
int n = pst.executeUpdate();
if (n > 0) {
JOptionPane.showMessageDialog(null, mensaje);
}
int to = Integer.parseInt(cantidad);
int to1=to-1;
gua = Integer.toString(to1);
sssSQL = "INSERT INTO tbl_soda(cantidad) VALUES(?)";
PreparedStatement pstt = cn.prepareStatement(sssSQL);
pstt.setString(1, cantidad);
int n1 = pstt.executeUpdate();
if (n1 > 0) {
JOptionPane.showMessageDialog(null, mensaje);
}
msj.setText("Se entregara enseguida su refresco");
} else if ("2".equals(val)) {
cantidad = rs.getString("cantidad");
int to = 1+Integer.parseInt(cantidad);
gua = Integer.toString(to);
ssSQL = "INSERT INTO tbl_soda(cantidad) VALUES(?)";
PreparedStatement pst = cn.prepareStatement(ssSQL);
pst.setString(1, cantidad);
int n = pst.executeUpdate();
if (n > 0) {
//cargarTablaMaestros("");
JOptionPane.showMessageDialog(null, mensaje);
}
msj.setText("Se esta agregando mas productos");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
} catch (Exception e) {
}
}
};
initComponents();
try {
Arduino.arduinoRX("COM4", 9600, listener);
} catch (Exception ex) {
JOptionPane.showConfirmDialog(null, ex);
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
msj = new javax.swing.JTextField();
dis = new javax.swing.JTextField();
ins = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
msj.setEditable(false);
msj.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
msjActionPerformed(evt);
}
});
dis.setEditable(false);
jLabel1.setText("Mini Simulacion de Maquina de Refresco");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addComponent(msj, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(dis, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ins, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(207, 207, 207)
.addComponent(jLabel1)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(msj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(dis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(ins, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(98, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void msjActionPerformed(java.awt.event.ActionEvent evt) {
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Mini_Proyecto_Principiante.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Mini_Proyecto_Principiante.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Mini_Proyecto_Principiante.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Mini_Proyecto_Principiante.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Mini_Proyecto_Principiante().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField dis;
private javax.swing.JTextField ins;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField msj;
// End of variables declaration
}