密码确认然后继续

时间:2014-11-30 01:39:31

标签: java swing jframe textfield indexoutofboundsexception

编辑: iam使用Netbeans 8.0.1

我一直在努力寻找解决方案,我尝试了jdialog和其他东西,并且无法帮助我希望你们帮助我。 我注意到在用户设置密码后我设置了fechadesalida = null,在我的数据库MySQL中,它会将所有文本字段保存为空格。

java应用程序启动时accion = "insertar"; 首先,我填充TextFields,然后单击此按钮

     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");
    }

}    

然后是另一个类jframe弹出窗口,这里用户插入他的密码然后bloun就是cicked

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


} 

这是方法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();
}

这是方法GuadarSalida();来自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);
    }

}

我遇到了这个问题,请帮助我一直在寻找并找不到解决方案

    Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.substring(String.java:1950) 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)

0 个答案:

没有答案