Netbeans - JTABLE NO MUESTRA LISTADO PEDIDOS

 
Vista:
Imágen de perfil de Alejandra
Val: 18
Ha disminuido 1 puesto en Netbeans (en relación al último mes)
Gráfica de Netbeans

JTABLE NO MUESTRA LISTADO PEDIDOS

Publicado por Alejandra (9 intervenciones) el 04/08/2020 00:25:58
Buenas noches:

me han pasado un código de un restaurante el cuál tuve que ir modificando para poder hacerlo funcionar, en su base de datos tiene las siguientes tablas:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
CREATE TABLE `pedido` (
  `numpedido` int not null primary key auto_increment,
  `fecha` varchar(10) DEFAULT NULL,
  `idempleado` int(4) not NULL,
   constraint `idemp`
   foreign key (`idempleado`) references `empleado`(`codempleado`),
  `idcliente` int(6) not NULL,
    constraint `idcli`
   foreign key (`idcliente`) references `cliente`(`codcliente`),
  `idmesa` int(6) not NULL,
   constraint `idme`
   foreign key (`idmesa`) references `mesa`(`codmesa`)
);
 
CREATE TABLE `detalle_pedido` (
  `idpedido` int(4) not null,
   constraint `idpe`
   foreign key (`idpedido`) references `pedido`(`numpedido`),
  `idcomida` int(4) not NULL,
   constraint `idcom`
   foreign key (`idcomida`) references `comida`(`codcomida`),
  `cantidad` tinyint(4) DEFAULT NULL,
   `precio` decimal(4,2) DEFAULT NULL
);

tiene un apartado en su menú principal que se llama listado de pedidos su código es el siguiente:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package Formularios;
import Base.ConectaBD;
import static Formularios.FrmConsultaPedido.jTblDetalle;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
 
public class FrmListadoPedidos extends javax.swing.JFrame {
//                DefaultTableModel model = (DefaultTableModel) FrmCampodeBusquedaPedido.jTblConsultaProductos.getModel();
 
    java.text.DecimalFormat formatoSalidaDecimal = new java.text.DecimalFormat("0.00");
    ConectaBD cnx = new ConectaBD();
    Date now = new Date(System.currentTimeMillis());
    SimpleDateFormat date = new SimpleDateFormat("dd/MM/yyyy");
    String sql = "Select * from v_Listar_Pedido_Fecha";
 
    public void LimpiarTabla(DefaultTableModel model) {
        for (int i = model.getRowCount() - 1; i >= 0; i--) {
            model.removeRow(i);
        }
    }
 
    public void icono() {
        Image icon = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Imagenes/003-navegador.png"));
        setIconImage(icon);
        setVisible(true);
    }
 
    public void LLenarTabla(String sql) {
        try {
            cnx.rs = cnx.stm.executeQuery(sql);
            DefaultTableModel model = (DefaultTableModel) this.jTblPedido.getModel();
            LimpiarTabla(model);
            while (cnx.rs.next()) {
                String col1 = cnx.rs.getString(1);
                String col2 = cnx.rs.getString(2);
                String col3 = cnx.rs.getString(3);
                String col4 = cnx.rs.getString(4);
                String col5 = cnx.rs.getString(5);
                String col6 = cnx.rs.getString(6);
 
                Vector vRow = new Vector();
                vRow.addElement(col1);
                vRow.addElement(col2);
                vRow.addElement(col3);
                vRow.addElement(col4);
                vRow.addElement(col5);
                vRow.addElement(col6);
                model.addRow(vRow);
            }
        } catch (SQLException a) {
            System.out.println(a);
        }
    }
 
    public void limpiarTabla(DefaultTableModel model) {
        for (int i = model.getRowCount() - 1; i >= 0; i--) {
            model.removeRow(i);
        }
    }
 
    public void Llenartabla1(String con_sql) {
        int Col11 = 1;
        try {
            cnx.rs = cnx.stm.executeQuery(con_sql);
            DefaultTableModel model = (DefaultTableModel) FrmCampodeBusquedaPedido.jTblConsultaProductos.getModel();
            limpiarTabla(model);
            while (cnx.rs.next()) {
 
                String Col0 = cnx.rs.getString(2);
                String Col1 = cnx.rs.getString(3);
                String Col2 = cnx.rs.getString(4);
 
                Vector vRow = new Vector();
                vRow.addElement(Col11);
                vRow.addElement(Col0);
                vRow.addElement(Col1);
                vRow.addElement(Col2);
                model.addRow(vRow);
                Col11++;
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
 
    public void MostrarProducto() {
        String cod, sql1;
        try {
            cod = this.jTblPedido.getValueAt(this.jTblPedido.getSelectedRow(), 0).toString();
            sql1 = "SELECT * FROM v_Listar_Pedido WHERE numpedido='" + cod + "'";
            String sql2 = "select * from v_Detalle_Pedido where idpedido='" + cod + "'";
            cnx.rs = cnx.stm.executeQuery(sql1);
            if (cnx.rs.next()) {
                FrmCampodeBusquedaPedido.jLblCodPedido.setText(cnx.rs.getString(1));
                FrmCampodeBusquedaPedido.jLblFecha.setText(cnx.rs.getString(2));
                FrmCampodeBusquedaPedido.jLblCliente.setText(cnx.rs.getString(4));
                FrmCampodeBusquedaPedido.jLblEmpleado.setText(cnx.rs.getString(3));
                FrmCampodeBusquedaPedido.jLblMesa.setText(cnx.rs.getString(5));
                Llenartabla1(sql2);
 
                double total = 0, Total = 0;
                for (int fila = 0; fila <= FrmCampodeBusquedaPedido.jTblConsultaProductos.getRowCount() - 1; fila++) {
                    String col2 = FrmCampodeBusquedaPedido.jTblConsultaProductos.getValueAt(fila, 2).toString();
                    String col3 = FrmCampodeBusquedaPedido.jTblConsultaProductos.getValueAt(fila, 3).toString();
                    total = Double.parseDouble(col3) * Integer.parseInt(col2);
                    Total = Total + total;
                }
                FrmCampodeBusquedaPedido.jLblMontoTotal.setText(String.valueOf(Total));
            }
        } catch (Exception a) {
            System.out.println(a.getMessage());
        }
    }
 
    public void CalcularTotal() {
        double total = 0;
        double ti;
        for (int fila = 0; fila <= this.jTblPedido.getRowCount() - 1; fila++) {
            total = total + Double.parseDouble(this.jTblPedido.getValueAt(fila, 5).toString());
        }
        formatoSalidaDecimal.format(total);
        this.jLblMonto.setText(String.valueOf(formatoSalidaDecimal.format(total)));
 
    }
 
    public FrmListadoPedidos() {
        initComponents();
        this.setLocationRelativeTo(this);
        this.setResizable(false);
        this.getContentPane().setBackground(Color.white);
    }
 
 
 
    private void JBTNMOSTRARActionPerformed(java.awt.event.ActionEvent evt) {
        String sql = "";
        if (this.JRBFecha.isSelected() == false && this.JRBEntre.isSelected() == false) {
            JOptionPane.showMessageDialog(null, "Selecione un Criterio de la Fecha", "Consulta de Empleados", JOptionPane.OK_OPTION);
            return;
        }
        if (this.JRBFecha.isSelected()) {
            sql = "SELECT * from  v_Listar_Pedido where fecha='" + date.format(this.jDFechaEspecifica.getDate()) + "'";
        } else {
            sql = "SELECT * FROM v_Listar_Pedido  where fecha Between '" + date.format(this.jDcEntre.getDate()) + "' and '" + date.format(this.jDcAl.getDate()) + "'";
        }
 
        this.LLenarTabla(sql);
        CalcularTotal();
    }
 
    private void JBTNSALIRActionPerformed(java.awt.event.ActionEvent evt) {
        int r;
        r = JOptionPane.showConfirmDialog(null, "Cerrar Programa", "Salir", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (r == JOptionPane.YES_OPTION) {
            dispose();
        }
    }
 
    private void formWindowOpened(java.awt.event.WindowEvent evt) {
        cnx.conectar();
        this.jDFechaEspecifica.setDate(now);
        this.jDcEntre.setDate(now);
        this.jDcAl.setDate(now);
        LLenarTabla(sql);
        CalcularTotal();
        icono();
    }
 
    private void JBTNSALIR1ActionPerformed(java.awt.event.ActionEvent evt) {
        LLenarTabla(sql);
        CalcularTotal();
        this.jDFechaEspecifica.setDate(now);
        this.jDcAl.setDate(now);
        this.jDcEntre.setDate(now);
    }
 
    private void jTblPedidoMouseClicked(java.awt.event.MouseEvent evt) {
        FrmCampodeBusquedaPedido frm_agregar_producto = new FrmCampodeBusquedaPedido();
        MostrarProducto();
        frm_agregar_producto.show();
    }
 
    private void formWindowActivated(java.awt.event.WindowEvent evt) {
        //cnx.conectar();
        //  CalcularTotal();
    }
 
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmListadoPedidos().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify
    private javax.swing.JButton JBTNMOSTRAR;
    private javax.swing.JButton JBTNSALIR;
    private javax.swing.JButton JBTNSALIR1;
    private javax.swing.JRadioButton JRBEntre;
    private javax.swing.JRadioButton JRBFecha;
    private javax.swing.ButtonGroup fecha;
    private com.toedter.calendar.JDateChooser jDFechaEspecifica;
    private com.toedter.calendar.JDateChooser jDcAl;
    private com.toedter.calendar.JDateChooser jDcEntre;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLblMonto;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTblPedido;
    // End of variables declaration
}


mi problema está en el método MostrarProducto() ya que así como está en el código no muestra nada, porque la tabla v_Listar_Pedido en la base de datos no está por lo que cree dicha tabla de acuerdo a los campos que figuran en el código:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CREATE TABLE `v_listar_pedido` (
  `numpedido` int(4) not null,
   constraint `numpe`
   foreign key (`numpedido`) references `pedido`(`numpedido`),
  `fecha` varchar(10) DEFAULT NULL,
  `idemple` int(4) not NULL,
   constraint `idempl`
   foreign key (`idemple`) references `empleado`(`codempleado`),
  `idclien` int(6) not NULL,
    constraint `idclie`
   foreign key (`idclien`) references `cliente`(`codcliente`),
  `idmes` int(6) not NULL,
   constraint `idm`
   foreign key (`idmes`) references `mesa`(`codmesa`),
   `TOTAL`decimal(4,2) DEFAULT NULL
);

y modifiqué el string sql1 del método v_listar_pedido de la siguiente manera:
1
2
3
4
sql1 = "SELECT lp.numpedido, lp.fecha, lp.idemple, lp.idclien, lp.idmes, lp.TOTAL"
                    + "from v_listar_pedido inner join empleado e on lp.idemple = e.codempleado "
                    + "inner join cliente C on lp.idclien = C.codcliente "
                    + "inner join mesa m on lp.idmes = m.codmesa where lp.numpedido='" + cod + "'";

pero tampoco me muestra nada, alguien puede ayudarme y decirme cuál es el error?
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Alejandra
Val: 18
Ha disminuido 1 puesto en Netbeans (en relación al último mes)
Gráfica de Netbeans

JTABLE NO MUESTRA LISTADO PEDIDOS

Publicado por Alejandra (9 intervenciones) el 05/08/2020 03:18:41
Reformulo mi pregunta:

¿Cómo puedo enviar el valor del jlabel jLblMontoTotal del jframe FrmPedido a otro jlabel jLblVentas ubicado en otro jframe llamado FrmMontoDelDia mostrarlo por pantalla (sin necesidad de un jbutton) y que a su vez este mismo jlabel jLblVentas vaya acumulando los montos de las ventas?



acumulador
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Alejandra
Val: 18
Ha disminuido 1 puesto en Netbeans (en relación al último mes)
Gráfica de Netbeans

JTABLE NO MUESTRA LISTADO PEDIDOS

Publicado por Alejandra (9 intervenciones) el 11/08/2020 14:12:12
Buen día: investigando por internet pude lograr mostrar el listado de los pedidos realizados en el programa del restaurante que me han pasado... el problema ahora es que al momento de realizar la consulta en MySQL en la columna TOTAL de la tabla listar_pedidos [dicha tabla contiene los mismos campos que pedidos] carga la misma con el valor que no es el monto de cada pedido realizado:


Captura-de-pantalla-95


Modifiqué la consulta del método MostrarProducto():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public void MostrarProducto() {
        String cod, sql1;
        try {
            cod = this.jTblPedido.getValueAt(this.jTblPedido.getSelectedRow(), 0).toString();
 
       //     sql1 = "SELECT * FROM listar_pedido WHERE numpedido='" + cod + "'";
 
            sql1 = " select distinct lp.numpedido as `nro_pedido`, lp.fecha as `fecha_pedido`, \n" +
                   " e.nombre as `empleado`, c.nombre as `cliente`, m.descripcion as `mesa`,\n" +
                   " (select round (det.cantidad * det.precio) from detalle_pedido as det LIMIT 1) AS `total`\n" +
                   " from listar_pedido as lp\n" +
                   " JOIN detalle_pedido AS det ON lp.numpedido = det.idpedido\n" +
                   " JOIN empleado AS e ON lp.idempleado = e.codempleado\n" +
                   " JOIN cliente AS c ON lp.idcliente = c.codcliente\n" +
                   " JOIN mesa AS m ON lp.idmesa = m.codmesa where lp.numpedido = '" + cod + "' ";
 
 
            String sql2 = "select * from detalle_pedido where idpedido='" + cod + "'";
            cnx.rs = cnx.stm.executeQuery(sql1);
 
            if (cnx.rs.next()) {
                FrmCampodeBusquedaPedido.jLblCodPedido.setText(cnx.rs.getString(1));
                FrmCampodeBusquedaPedido.jLblFecha.setText(cnx.rs.getString(2));
                FrmCampodeBusquedaPedido.jLblCliente.setText(cnx.rs.getString(4));
                FrmCampodeBusquedaPedido.jLblEmpleado.setText(cnx.rs.getString(3));
                FrmCampodeBusquedaPedido.jLblMesa.setText(cnx.rs.getString(5));
                Llenartabla1(sql2);
 
                double total = 0, Total = 0;
                for (int fila = 0; fila <= FrmCampodeBusquedaPedido.jTblConsultaProductos.getRowCount() - 1; fila++) {
                    String col2 = FrmCampodeBusquedaPedido.jTblConsultaProductos.getValueAt(fila, 2).toString();
                    String col3 = FrmCampodeBusquedaPedido.jTblConsultaProductos.getValueAt(fila, 3).toString();
                    total = Double.parseDouble(col3) * Integer.parseInt(col2);
                    Total = Total + total;
                }
                FrmCampodeBusquedaPedido.jLblMontoTotal.setText(String.valueOf(Total));
            }
        } catch (Exception a) {
            System.out.println(a.getMessage());
        }
    }
 
    public void CalcularTotal() {
        double total = 0;
        double ti;
        for (int fila = 0; fila <= this.jTblPedido.getRowCount() - 1; fila++) {
            total = total + Double.parseDouble(this.jTblPedido.getValueAt(fila, 5).toString());
        }
        formatoSalidaDecimal.format(total);
        this.jLblMonto.setText(String.valueOf(formatoSalidaDecimal.format(total)));
 
    }

¿qué estoy haciendo mal para que no muestre los totales de cada pedido realizado?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar