Why can not I concatenate a string to the SQL statement?

1

Good afternoon someone could help me with this little problem please ... I have 2 functions, 1a) SearchIdFactura () obtains the id of the last record of the "invoice" table and stores it in a global variable "string idFactura". The 2nd) ShowTable () makes a query and the data obtained shows them in the table. My problem is that in the query sql that is in the function ShowTable () I concateno the variable "idFactura" obtained with the first function but it does not show results because it is null but I already verify the function BucarIdFactura () and if it saves the idFact correctly. I find it very strange because I have previously concatenated several sentences and I have not had any problems. This is the sentence that does not work for me:

String sql="SELECT Cantidad, Nombre, Precio FROM desarrollo_venta dv JOIN producto pd ON dv.ID_Producto=pd.ID_Producto WHERE ID_DesVent="+idFactura+";";

This is the code:

Conectar con = new Conectar();
Connection conn = con.getConnection();
String idFactura;

public facturas() {
    initComponents();
    BuscarIdFactura();
    MostrarTabla();
}


void BuscarIdFactura(){
     Connection con;
    //Aqui busco el campo cantidad del ID_DesVent que obtuve con la sentencia anterior
      try {
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/susyken", "root", "");
        String sentencia = "SELECT MAX(ID_Factura) FROM factura";
        Statement stmt = null; //Esto sustituirá a ps
        stmt = con.createStatement(); //Usamos create... no prepare...
        ResultSet rs = stmt.executeQuery(sentencia);
        while (rs.next()) {
            idFactura = rs.getString(1);
        }
      } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "Error");
      }
}


void MostrarTabla(){

    DefaultTableModel modelo = new DefaultTableModel();
    modelo.addColumn("Cantidad");
    modelo.addColumn("Producto");
    modelo.addColumn("Precio");
    jtFactura.setModel(modelo);

    String sql="SELECT Cantidad, Nombre, Precio FROM desarrollo_venta dv JOIN producto pd ON dv.ID_Producto=pd.ID_Producto WHERE ID_DesVent="+idFactura+";";
    String datos[] = new String[3];
    Statement st;
    try {
        st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        while(rs.next()){
            datos[0] = rs.getString(1);
            datos[1] = rs.getString(2);
            datos[2] = rs.getString(3);
            modelo.addRow(datos);
        }
        jtFactura.setModel(modelo);

    } catch (SQLException ex) {
        Logger.getLogger(facturas.class.getName()).log(Level.SEVERE, null, ex);
    }
    
asked by Javier Saavedra 22.10.2018 в 17:37
source

1 answer

0

you must put before the double quotes "" the single quotes '' so it should work for you, I hope it works for you and where in the where ID_DesVent you should put the field with the invoice id column and you are done

String sql="SELECT Cantidad, Nombre, Precio FROM desarrollo_venta dv JOIN producto pd ON dv.ID_Producto=pd.ID_Producto WHERE ID_DesVent='"+idFactura+"';";
    
answered by 22.10.2018 в 17:42