I am trying to add data to a database using a button, but at the moment of entering the data I get an error in the Prepared Statement part and my SQL statement I tried it on my local server (phpmyadmin) and it worked perfection, and I have not been able to advance in my project because of that error.
I leave part of my code:
This is when you click the add button to the database
conexionBD cc = new conexionBD();
Connection cn=cc.conectar;
String sql="";
if(rbnHerraOfi.isSelected())
tip=rbnHerraOfi.getText();
else
tip=rbnArtPape.getText();
sql="INSERT INTO productos (Articulo,Precio_compra,Precio_venta,Stock_min,TipoArticulo,Cantidad) VALUES (?,?,?,?,?,?)";
try {
PreparedStatement pst = cn.prepareStatement(sql);
pst.setString(1,txt_NombrePro.getText());
pst.setDouble(2,Double.parseDouble(txtPrecioCompraPro.getText()));
pst.setDouble(3,Double.parseDouble(txtPrecioVentaPro.getText()));
pst.setInt(4,Integer.parseInt(txtStockMinimo.getText()));
pst.setString(5,tip);
pst.setInt(6,Integer.parseInt(txtCantidadPro.getText()));
int n=pst.executeUpdate();
if(n>=0){
JOptionPane.showMessageDialog(null,"Registros Guardados con Exito");
limpiar();
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,""+ex);
}
'
And this is what I have in my connection class
public class conexionBD {
Connection conectar=null;
public Connection conectar() {
try {
Class.forName("org.gjt.mm.mysql.Driver");
conectar=DriverManager.getConnection("jdbc:mysql://localhost/papeleria","root","");
JOptionPane.showMessageDialog(null, "Conectado correctamente");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error al conectarse a la base de datos"+e);
}
return conectar;
}
}
This is the error I receive:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at pepeleria2.Admin2.btnGuardarProActionPerformed (Admin2.java:650)
Line 650 is exactly where the PreparedStatement
is.