Boa tarde amigos.
estou com problema na hora de persistir um formattedtextfield, estou passando ele para o simpledateformat e realizando cast mas mesmo assim ele continua dando erro na hora de persistir.
Alguem??
Segue os codigos
package viewer;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;
import model.DepartamentoDAO;
import model.EmpregadoDAO;
public class CadastroEmpregado extends JFrame {
private JPanel contentPane;
private JTextField textFieldEnd;
private JTextField textFieldNome;
private JFormattedTextField formattedTelefone;
private JFormattedTextField formattedRg;
private JFormattedTextField formattedData;
private JComboBox comboBox;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CadastroEmpregado frame = new CadastroEmpregado();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* @throws ParseException
*/
public CadastroEmpregado() throws ParseException {
setResizable(false);
setTitle("Cadastro Empregado");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 449, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNome = new JLabel("Nome");
lblNome.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblNome.setBounds(10, 30, 46, 23);
contentPane.add(lblNome);
JLabel lblDataDeNascimento = new JLabel("Data de Nascimento");
lblDataDeNascimento.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblDataDeNascimento.setBounds(10, 55, 144, 23);
contentPane.add(lblDataDeNascimento);
JLabel lblRg = new JLabel("RG");
lblRg.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblRg.setBounds(10, 80, 46, 23);
contentPane.add(lblRg);
JLabel lblTelefone = new JLabel("Telefone");
lblTelefone.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblTelefone.setBounds(10, 114, 73, 20);
contentPane.add(lblTelefone);
JLabel lblEndereo = new JLabel("Endere\u00E7o");
lblEndereo.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblEndereo.setBounds(10, 145, 73, 23);
contentPane.add(lblEndereo);
JLabel lblSiglaDepartamento = new JLabel("Sigla Departamento");
lblSiglaDepartamento.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblSiglaDepartamento.setBounds(10, 170, 144, 23);
contentPane.add(lblSiglaDepartamento);
JButton btnCancelar = new JButton("Cancelar");
btnCancelar.setFont(new Font("Tahoma", Font.PLAIN, 15));
btnCancelar.setBounds(344, 237, 89, 23);
contentPane.add(btnCancelar);
JButton btnAplicar = new JButton("Aplicar");
btnAplicar.setFont(new Font("Tahoma", Font.PLAIN, 15));
btnAplicar.setBounds(245, 237, 89, 23);
contentPane.add(btnAplicar);
textFieldEnd = new JTextField();
textFieldEnd.setFont(new Font("Tahoma", Font.PLAIN, 15));
textFieldEnd.setBounds(87, 144, 346, 25);
contentPane.add(textFieldEnd);
textFieldEnd.setColumns(10);
formattedTelefone = new JFormattedTextField(new MaskFormatter("##-####-####"));
formattedTelefone.setHorizontalAlignment(SwingConstants.CENTER);
formattedTelefone.setFont(new Font("Tahoma", Font.PLAIN, 15));
formattedTelefone.setBounds(87, 113, 171, 25);
contentPane.add(formattedTelefone);
formattedRg = new JFormattedTextField(new MaskFormatter("##.###.###-#"));
formattedRg.setHorizontalAlignment(SwingConstants.CENTER);
formattedRg.setFont(new Font("Tahoma", Font.PLAIN, 15));
formattedRg.setBounds(87, 83, 163, 25);
contentPane.add(formattedRg);
formattedData = new JFormattedTextField(new MaskFormatter("##-##-####"));
formattedData.setHorizontalAlignment(SwingConstants.CENTER);
formattedData.setFont(new Font("Tahoma", Font.PLAIN, 15));
formattedData.setBounds(148, 55, 110, 25);
contentPane.add(formattedData);
textFieldNome = new JTextField();
textFieldNome.setFont(new Font("Tahoma", Font.PLAIN, 15));
textFieldNome.setBounds(55, 30, 378, 23);
contentPane.add(textFieldNome);
textFieldNome.setColumns(10);
comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new Vector<String>(new DepartamentoDAO().listaSigla())));
comboBox.setFont(new Font("Tahoma", Font.PLAIN, 13));
comboBox.setBounds(148, 173, 63, 23);
contentPane.add(comboBox);
btnAplicar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (comboBox.getSelectedItem().equals(null)){
JOptionPane.showMessageDialog(null, "Por favor cadastre um Departamento antes de prosseguir");
}else{
SimpleDateFormat dateF = new SimpleDateFormat("DD-MM-YYYY");
Date dataNasc;
try {
dataNasc = (Date) dateF.parse(formattedData.getText());
new EmpregadoDAO().addEmpregado(textFieldNome.getText(),dataNasc,formattedRg.getSelectedText(),formattedTelefone.getSelectedText(),textFieldEnd.getText(),String.valueOf(comboBox.getSelectedItem()));
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
});
}
}
E da DAO
package model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import controler.Conexao;
public class EmpregadoDAO {
public Connection con = Conexao.getInstance();
public void addEmpregado(String stg01, Date date01, String stg02, String stg03,String stg04,String stg05){
String sql = "INSERT INTO EMPREGADO (nome, data_nascimento, rg, telefone, endereco, sigla) values (?,?,?,?,?,?)";
PreparedStatement pstmt;
try {
pstmt = con.prepareStatement(sql);
pstmt.setString(1, stg01);
pstmt.setDate(2, date01);
pstmt.setString(3, stg02);
pstmt.setString(4, stg03);
pstmt.setString(5, stg04);
pstmt.setString(6, stg05);
pstmt.execute();
pstmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public ArrayList<Empregado> recuperaEmpregado(String stg01){
String sql;
sql = "SELECT * FROM EMPREGADO WHERE RG = ?";
PreparedStatement pstmt;
ResultSet rs;
ArrayList<Empregado> array = null;
Empregado empTeste = new Empregado();
try {
pstmt = con.prepareStatement(sql);
pstmt.setString(1, stg01);
rs = pstmt.executeQuery();
while (rs.next()) {
empTeste.setCod_empregado(rs.getInt(1));
empTeste.setNome(rs.getString(2));
empTeste.setData_nascimento(rs.getString(3));
empTeste.setRg(rs.getString(4));
empTeste.setTelefone(rs.getString(5));
empTeste.setEndereço(rs.getString(6));
empTeste.setSiglaDepto(rs.getString(7));
array.add(empTeste);
}
pstmt.close();
return array;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Segue o erro também
Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
at viewer.CadastroEmpregado$2.actionPerformed(CadastroEmpregado.java:156)
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$200(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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)