Insert the Password to continue

Asked

Viewed 156 times

6

Note: I do not speak Portuguese. This text was translated before it was posted and the translation was revised by other users.

I am using Netbeans 8.0.1.

I’ve been looking for a solution for days. I’ve tried to use JDialog and other things, but it didn’t work.

I noticed that after the user sets the passwords and I set the fechadesalida = null, in my Mysql database will save the entire text field, such as empty spaces.

when the java application starts Accion = "insert"; First of all I fill the TextFields and I click this button:

private void btnGuardarSalidaActionPerformed(java.awt.event.ActionEvent evt) {                                                 

    if (cboCamion.getSelectedItem() != null) {
        if (cboChofer.getSelectedItem() != null) {
            if (T_SalidaKm.getText().trim().length() != 0) {
                if (T_FechaSalida.getText().trim().length() != 0) {
                    ChoferLogin Chofer = new ChoferLogin();
                    Chofer.a = ((String) cboChofer.getSelectedItem());
                    Chofer.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(null, "Inserte la Fecha");
                }
            } else {
                JOptionPane.showMessageDialog(null, "Seleccione el Km Salida");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Seleccione el Chofer");
        }
    } else {
        JOptionPane.showMessageDialog(null, "Seleccione el Camion");
    }

}    

Then another JFrame appears, and here the user enters his password and the button is clicked.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    ChoferLog();
}

This is the way ChoferLog:

public void ChoferLog() {

    String sql4 = " Select * from choferes where chofer=? and Password=?";

    try {
        pst = con.prepareStatement(sql4);
        pst.setString(1, a);
        pst.setString(2, T_Password.getText());

        rs = pst.executeQuery();

        if (rs.next()) {
            JOptionPane.showMessageDialog(null, "Confirmado");
            viajes.GuardarSalida();
            this.dispose();

        } else {
            JOptionPane.showMessageDialog(null, "La Contrasena es incorrecto");
        }

    } catch (SQLException | HeadlessException e) {
        JOptionPane.showMessageDialog(null, "Error" + e);
    }

    dispose();
}

And this is the method GuadarSalida class Viajes:

void GuardarSalida() {
    switch (accion) {
        case "insertar":
            sql = "insert into viajes(camion,chofer,kmsalida,acompanhante,"
                    + "viatico,destino,cargadediesel,litros,fechadesalida,"
                    + "bascula,peaje)"
                    + " values (?,?,?,?,?,?,?,?,?,?,?) ";
            break;
        case "modificar":
            sql = "Update viajes set "
                    + "camion= ?,"
                    + "chofer= ?,"
                    + "kmsalida= ?,"
                    + "acompanhante= ?,"
                    + "viatico= ?,"
                    + "destino= ?,"
                    + "cargadediesel= ?,"
                    + "litros= ?,"
                    + "fechadesalida= ? ,"
                    + "bascula= ?,"
                    + "peaje= ? "
                    + " where idviajes = " + id;
            break;
    }
    sql = "insert into viajes(camion,chofer,kmsalida,acompanhante,"
            + "viatico,destino,cargadediesel,litros,fechadesalida,"
            + "bascula,peaje)"
            + " values (?,?,?,?,?,?,?,?,?,?,?) ";

    try {

        day = T_FechaSalida.getText().substring(0, 2);
        month = T_FechaSalida.getText().substring(3, 5);
        year = T_FechaSalida.getText().substring(6);
        pst = con.prepareStatement(sql);
        pst.setString(1, (String) cboCamion.getSelectedItem());
        pst.setString(2, (String) cboChofer.getSelectedItem());
        pst.setString(3, T_SalidaKm.getText());
        pst.setString(4, (String) cboAcompanhante.getSelectedItem());
        pst.setString(5, T_Viatico.getText());
        pst.setString(6, T_Destino.getText());
        pst.setString(7, T_CargadeDiesel.getText());

        if (T_CantDiesel.getText().isEmpty()) {
            pst.setString(8, null);
        } else {
            pst.setString(8, T_CantDiesel.getText());
        }

        pst.setString(9, year + "-" + month + "_" + day);
        pst.setString(10, T_Bascula.getText());
        pst.setString(11, T_Peaje.getText());
        pst.executeUpdate();

        TablaDeEspera();
        LimpiarSalida();
        JOptionPane.showMessageDialog(null, "Se Cargo Con Exito !", "Guardar", JOptionPane.INFORMATION_MESSAGE);
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "No Se Cargo Los Datos " + ex);
    }

}

And I get this exception. Please help me, I’ve been looking for days and we can’t find the solution:

Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
    at java.lang.String.substring(String.java:1950)
    at Viaje.Viajes.GuardarSalida(Viajes.java:353)
    at Viaje.ChoferLogin.ChoferLog(ChoferLogin.java:61)
    at Viaje.ChoferLogin.jButton1ActionPerformed(ChoferLogin.java:197)
    at Viaje.ChoferLogin.access$300(ChoferLogin.java:22)
    at Viaje.ChoferLogin$3.actionPerformed(ChoferLogin.java:116)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
  • According to the generated exception, the substring method is not finding the Index [1] probably the T_fechasalida field does not have 2 characters, the substring reading(0, 2) would be (from position 0, take up to position 2 - 1), translating, it will pick up the position [0], [1]

1 answer

1

It seems to me that your mistake comes from here:

day = T_FechaSalida.getText().substring(0, 2);

That is, the countryside T_FechaSalida has not been filled in correctly.

You are not testing anywhere if the fields form filled in correctly.

Modify the method btnGuardarSalidaActionPerformed to check that all fields are filled in correctly (or put this validation elsewhere) before sending it to the database.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.