Note: I do not speak Portuguese. This text was translated before it was posted and the translation was reviewed by other users.
I'm using Netbeans 8.0.1.
For days I've been looking for a solution to this. I've already tried JDialog
and stuff, but it did not work.
I noticed that after the user sets the passwords and I set the fechadesalida = null
, in my MySQL database it will save the entire field of text as empty spaces.
when the java application starts action="insert";
First of all I pre-set the TextFields
and click on 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 ChoferLog
method:
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 GuadarSalida
method of 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 have been searching for days and we can not 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)