Olá galera, eu preciso de ajuda na seguinte forma: eu preciso verificar se todos os campos obrigatórios estão preenchidos, se estiverem, os dados deles são incluídos no banco, senão, aparece uma mensagem falando pra conferir os campos e a ação do botão é cancelada. Eu fiz da assim:
public void btnIncluir_actionPerformed(ActionEvent e) throws ParseException {
dao.conectar();
if((textFieldCEP.getText().length()>0) && (textFieldRua.getText().length()>0) && (textFieldSetor.getText().length()>0) && (textFieldCidade.getText().length()>0) && (comboBoxUF.getSelectedItem().equals(""))
&& (textFieldRA.getText().length()>0) && (textFieldNome.getText().length()>0) && (textFieldMae.getText().length()>0) &&(textFieldNomeCurso.getText().length()>0)
&& (textFieldMatri.getText().length()>0) && (textFieldNasc.getText().length()>0) && (textFieldCidadeNa.getText().length()>0) && (comboBoxUFN.getSelectedItem().equals(""))) {
bean.setRA(textFieldRA.getText());
bean.setNome(textFieldNome.getText());
bean.setNomePai(textFieldPai.getText());
bean.setNomeMae(textFieldMae.getText());
bean.setNomeCurso(textFieldNomeCurso.getText());
String srtData=String.valueOf(textFieldMatri.getText());
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date = format.parse(srtData);
long mili = date.getTime();
java.sql.Date sqlDate = new java.sql.Date(mili);
bean.setDataMatricula(sqlDate);
bean.setRua(textFieldRua.getText());
bean.setSetor(textFieldSetor.getText());
bean.setCidade(textFieldCidade.getText());
bean.setUF(String.valueOf(comboBoxUF.getSelectedItem()));
bean.setCEP(Integer.parseInt(textFieldCEP.getText()));
String srtData2=String.valueOf(textFieldNasc.getText());
SimpleDateFormat format2 = new SimpleDateFormat("dd/MM/yyyy");
Date date2 = format2.parse(srtData2);
long mili2 = date2.getTime();
java.sql.Date sqlDate2 = new java.sql.Date(mili2);
bean.setDataNascimento(sqlDate2);
bean.setCidadeNascimento(textFieldCidadeNa.getText());
bean.setUFNascimento(String.valueOf(comboBoxUFN.getSelectedItem()));
dao.inserir(bean);
JOptionPane.showMessageDialog(null, "Dados cadastrados com sucesso!");
dao.fecharCon();
}else{
JOptionPane.showMessageDialog(null, "Preêncha todos os campos obrigatórios (marcados com *)");
}
}public void btnIncluir_actionPerformed(ActionEvent e) throws ParseException {
dao.conectar();
if((textFieldCEP.getText().length()>0) && (textFieldRua.getText().length()>0) && (textFieldSetor.getText().length()>0) && (textFieldCidade.getText().length()>0) && (comboBoxUF.getSelectedItem().equals(""))
&& (textFieldRA.getText().length()>0) && (textFieldNome.getText().length()>0) && (textFieldMae.getText().length()>0) &&(textFieldNomeCurso.getText().length()>0)
&& (textFieldMatri.getText().length()>0) && (textFieldNasc.getText().length()>0) && (textFieldCidadeNa.getText().length()>0) && (comboBoxUFN.getSelectedItem().equals(""))) {
bean.setRA(textFieldRA.getText());
bean.setNome(textFieldNome.getText());
bean.setNomePai(textFieldPai.getText());
bean.setNomeMae(textFieldMae.getText());
bean.setNomeCurso(textFieldNomeCurso.getText());
String srtData=String.valueOf(textFieldMatri.getText());
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date date = format.parse(srtData);
long mili = date.getTime();
java.sql.Date sqlDate = new java.sql.Date(mili);
bean.setDataMatricula(sqlDate);
bean.setRua(textFieldRua.getText());
bean.setSetor(textFieldSetor.getText());
bean.setCidade(textFieldCidade.getText());
bean.setUF(String.valueOf(comboBoxUF.getSelectedItem()));
bean.setCEP(Integer.parseInt(textFieldCEP.getText()));
String srtData2=String.valueOf(textFieldNasc.getText());
SimpleDateFormat format2 = new SimpleDateFormat("dd/MM/yyyy");
Date date2 = format2.parse(srtData2);
long mili2 = date2.getTime();
java.sql.Date sqlDate2 = new java.sql.Date(mili2);
bean.setDataNascimento(sqlDate2);
bean.setCidadeNascimento(textFieldCidadeNa.getText());
bean.setUFNascimento(String.valueOf(comboBoxUFN.getSelectedItem()));
dao.inserir(bean);
JOptionPane.showMessageDialog(null, "Dados cadastrados com sucesso!");
dao.fecharCon();
}else{
JOptionPane.showMessageDialog(null, "Preêncha todos os campos obrigatórios (marcados com *)");
}
}
Porém, se eu clico no botão de incluir com todos os campos vazios, dá um erro :
java.text.ParseException: Unparseable date: " / / "
at java.text.DateFormat.parse(Unknown Source)
at np2.ManterAlunos.btnIncluir_actionPerformed(ManterAlunos.java:665)
at np2.ManterAlunos$6.actionPerformed(ManterAlunos.java:305)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
java.text.ParseException: Unparseable date: " / / "
at java.text.DateFormat.parse(Unknown Source)
at np2.ManterAlunos.btnIncluir_actionPerformed(ManterAlunos.java:665)
at np2.ManterAlunos$6.actionPerformed(ManterAlunos.java:305)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
java.text.ParseException: Unparseable date: " / / "
at java.text.DateFormat.parse(Unknown Source)
at np2.ManterAlunos.btnIncluir_actionPerformed(ManterAlunos.java:665)
at np2.ManterAlunos$6.actionPerformed(ManterAlunos.java:305)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Agradeço desde já a atenção!