Select query with JComboBox

0

Hi I have a problem I want to make a JComboBox I can select an article from my BD and I do the query to load well and everything but when I want to select another article the query that I have to look at the stock of said article fails me here I leave the code if someone tells me: (

WITH THIS CHARGE THE DATA (I FEEL ME THAT IS WELL)

//limpio el combobox y el textfield
txtProducto.removeAllItems();
txtInventario.setText("");
try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection conne=(Connection)DriverManager.getConnection(url,login,password);
    Statement consulta=(Statement) conne.createStatement();

    //consulta sql a la tabla 
    ResultSet rsinventario = consulta.executeQuery ("select Descripcion from inventario");


    // Se recorre el ResultSet inventario.
    while (rsinventario.next()) {
        txtProducto.addItem((String) rsinventario.getObject("Descripcion"));
    }

    ResultSet rscantidad = consulta.executeQuery ("select Cantidad from inventario");
    while(rscantidad.next()) {
        txtInventario.setText((String) rscantidad.getObject("Cantidad"));
    }

    conne.close();
} catch(SQLException e){
    JOptionPane.showMessageDialog(null,"Error sql no se pueden leer datos");
} catch(ClassNotFoundException e){
    JOptionPane.showMessageDialog(null,"Error al leer la base de datos");
}

AND WITH THIS I MAKE THE CONSULTATION WHICH MAKES ME WHEN I SELECT ANY ARTICLE SHOW ME THE STOCK OF SAID IN A JTEXTFIELD (I MISS AN ERROR WITH THE WHERE IT CORRECTS MY NETBEANS WELL BUT IN THE CONSOLE IT LEAVES ERROR BELOW THE DETAIL BETTER )

ConexionesMySQL mysql = new ConexionesMySQL();
Connection cn = mysql.conectar();
try {
    Statement st= cn.createStatement();
    ResultSet rs = st.executeQuery("select * from inventario where Descripcion="+txtProducto.getSelectedItem()+"");
    while(rs.next())
    {
    txtInventario.setText((String) rs.getObject("Cantidad"));
    }
}
catch (SQLException ex) {
    Logger.getLogger(Hojas_Envio.class.getName()).log(Level.SEVERE, null, ex);
}

txtProduct is the name of my jcombobox I want the query to make me what is selected and when the program is run and selected this error comes out in console

(Calcetas is the name of an article that is in my BD if it works it should come out that I have 56 in stock :()

  

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Socks' in 'where clause'

I have started in this programming and the truth if they told me to fail I would be very helpful

    
asked by PartyHard 14.08.2017 в 02:16
source

1 answer

0

It says that the column "Calcetas" does not exist, I think the error is in the nesting of your query "select * from inventory where Description=" + txtProduct.getSelectedItem () + "" "

    
answered by 14.08.2017 в 04:21