Boa noite pessoal.
Estou desenvolvendo um projeto que necessita do banco de dados.
Eu consegui fazer a conexão tudo certo, porem quando eu salvo as informações no banco da o seguinte erro.
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column ‘fun_nome’ cannot be null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3978)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3914)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2495)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1903)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1242)
at DAO.FuncionarioDAO.inserir(FuncionarioDAO.java:26)
at GUI.FuncionarioTela.BtsaveActionPerformed(FuncionarioTela.java:268)
at GUI.FuncionarioTela.access$600(FuncionarioTela.java:12)
at GUI.FuncionarioTela$8.actionPerformed(FuncionarioTela.java:134)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
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.Component.processMouseEvent(Component.java:6539)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6304)
at java.awt.Container.processEvent(Container.java:2239)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
at java.awt.Container.dispatchEventImpl(Container.java:2283)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
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)
Segue os códigos:
CADASTRO DE FUNCIONÁRIOS:
package GUI;
import Beans.FuncionarioBeans;
import Controller.FuncionarioController;
import DAO.FuncionarioDAO;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.MaskFormatter;
public class FuncionarioTela extends javax.swing.JFrame {
MaskFormatter FormatoTel;
SimpleDateFormat FormatoData;
FuncionarioBeans funcionarioB = new FuncionarioBeans();
FuncionarioDAO funcionarioD;
FuncionarioController funcC;
public FuncionarioTela() {
initComponents();
FormatoData = new SimpleDateFormat("dd/MM/yyyy");
funcC = new FuncionarioController();
funcionarioB = new FuncionarioBeans();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel4 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel2 = new javax.swing.JLabel();
txtCodigo = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
txtNome = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
cb_cargo = new javax.swing.JComboBox<>();
jLabel5 = new javax.swing.JLabel();
txtData = new javax.swing.JTextField();
jLabel7 = new javax.swing.JLabel();
txtBuscar = new javax.swing.JTextField();
jScrollPane4 = new javax.swing.JScrollPane();
tbResult = new javax.swing.JTable();
jScrollPane5 = new javax.swing.JScrollPane();
jScrollPane6 = new javax.swing.JScrollPane();
btAdd = new javax.swing.JButton();
Btsave = new javax.swing.JButton();
btBuscar = new javax.swing.JButton();
msgresult = new javax.swing.JLabel();
lblMesnagem = new javax.swing.JLabel();
jLabel4.setText("jLabel4");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Cadastro Funconários");
jLabel2.setText("Código");
txtCodigo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtCodigoActionPerformed(evt);
}
});
jLabel1.setText("Nome");
txtNome.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtNomeActionPerformed(evt);
}
});
jLabel3.setText("Cargo");
cb_cargo.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Selecione um cargo", "Atendente", "Gerente", "Administrador" }));
cb_cargo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cb_cargoActionPerformed(evt);
}
});
jLabel5.setText("Data");
txtData.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtDataActionPerformed(evt);
}
});
jLabel7.setText("Buscar");
txtBuscar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtBuscarActionPerformed(evt);
}
});
tbResult.setAutoCreateRowSorter(true);
tbResult.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Código", "Nome", "Cargo", "Data de Cad."
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
jScrollPane4.setViewportView(tbResult);
btAdd.setText("Adicionar");
btAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btAddActionPerformed(evt);
}
});
Btsave.setText("Salvar");
Btsave.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtsaveActionPerformed(evt);
}
});
btBuscar.setText("Buscar");
btBuscar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btBuscarActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(cb_cargo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel5)))
.addGap(10, 10, 10)
.addComponent(txtData, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
.addComponent(jScrollPane5)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addComponent(jScrollPane6)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(btAdd)
.addGap(46, 46, 46)
.addComponent(btBuscar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Btsave)
.addGap(34, 34, 34))
.addGroup(layout.createSequentialGroup()
.addComponent(lblMesnagem, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(msgresult, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(9, 9, 9)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(cb_cargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5)
.addComponent(txtData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(17, 17, 17)
.addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(46, 46, 46)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btAdd)
.addComponent(Btsave)
.addComponent(btBuscar))
.addGap(26, 26, 26)
.addComponent(msgresult)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lblMesnagem)
.addGap(27, 27, 27))
);
pack();
}// </editor-fold>
private void txtDataActionPerformed(java.awt.event.ActionEvent evt) {
}
private void cb_cargoActionPerformed(java.awt.event.ActionEvent evt) {
}
private void txtNomeActionPerformed(java.awt.event.ActionEvent evt) {
}
private void txtCodigoActionPerformed(java.awt.event.ActionEvent evt) {
}
private void txtBuscarActionPerformed(java.awt.event.ActionEvent evt) {
}
private void btAddActionPerformed(java.awt.event.ActionEvent evt) {
java.util.Date DataAtual = new java.util.Date();
txtData.setText(FormatoData.format(DataAtual));
habilitarCampos(true);
habilitarBusca(false);
}
private void BtsaveActionPerformed(java.awt.event.ActionEvent evt) {
String nome = funcionarioB.getnome();
funcionarioB.setnome(nome);
habilitarCampos(false);
try {
funcionarioD.inserir();
} catch (ClassNotFoundException ex) {
Logger.getLogger(FuncionarioTela.class.getName()).log(Level.SEVERE, null, ex);
}
Limpar();
}
private void btBuscarActionPerformed(java.awt.event.ActionEvent evt) {
habilitarBusca(true);
habilitarCampos(false);
Limpar();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FuncionarioTela().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Btsave;
private javax.swing.JButton btAdd;
private javax.swing.JButton btBuscar;
private javax.swing.JComboBox<String> cb_cargo;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel7;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JScrollPane jScrollPane5;
private javax.swing.JScrollPane jScrollPane6;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JLabel lblMesnagem;
private javax.swing.JLabel msgresult;
private javax.swing.JTable tbResult;
private javax.swing.JTextField txtBuscar;
private javax.swing.JTextField txtCodigo;
private javax.swing.JTextField txtData;
private javax.swing.JTextField txtNome;
// End of variables declaration
final void habilitarBusca(boolean valor) {
txtBuscar.setEnabled(valor);
}
final void habilitarCampos(boolean valor) {
txtNome.setEnabled(valor);
cb_cargo.setEnabled(valor);
txtData.setEnabled(valor);
txtCodigo.setEnabled(valor);
tbResult.setEnabled(valor);
}
final void popularFuncionarioBeans() {
funcionarioB.setnome(txtNome.getText());
funcionarioB.setcargo(cb_cargo.getSelectedItem().toString());
funcionarioB.setData_Cadastro(txtData.getText());
}
public void Limpar() {
txtCodigo.setText("");
txtNome.setText("");
cb_cargo.setSelectedIndex(0);
txtData.setText("");
}
}
CONSTRUTOR:
package Beans;
public class FuncionarioBeans {
private int codigo;
private String nome;
private String cargo;
private String Data_Cadastro;
public FuncionarioBeans() {
}
public int getcodigo() {
return codigo;
}
public void setcodigo(int cod) {
this.codigo = cod;
}
public String getnome() {
return nome;
}
public void setnome(String nome) {
this.nome = nome;
}
public String getcargo() {
return cargo;
}
public void setcargo(String Cargo) {
this.cargo = Cargo;
}
public String getData_Cadastro() {
return Data_Cadastro;
}
public void setData_Cadastro(String Data_Cadastro) {
this.Data_Cadastro = Data_Cadastro;
}
}
CLASSE DAO:
package DAO;
import Beans.FuncionarioBeans;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class FuncionarioDAO {
public static void inserir() throws ClassNotFoundException {
FuncionarioBeans funcionarioBeans = new FuncionarioBeans();
try {
Connection connection = ConexaoUtil.getInstance().getConnection();
String sql = "INSERT INTO funcionários(fun_nome, fun_cargo, fun_data_cadastro) VALUES(?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, funcionarioBeans.getnome());
statement.setString(2, funcionarioBeans.getcargo());
statement.setString(3, funcionarioBeans.getData_Cadastro());
statement.execute();
// connection.commit();
JOptionPane.showMessageDialog(null, “Registro salvo com sucesso”);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void remover(int fun_cod) {
try {
Connection connection = ConexaoUtil.getInstance().getConnection();
String sql = "DELETE FROM funcionários WHERE fun_cod = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, fun_cod);
} catch (Exception e) {
e.printStackTrace();
}
}
public String Listar() throws ClassNotFoundException, SQLException {
try {
Connection connection = ConexaoUtil.getInstance().getConnection();
String sql = "SELECT * FROM funcionário";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet rs = statement.executeQuery();
if (rs.next()) {
return (Integer.parseInt(rs.getString("fun_cod")) + 1) + "";
} else {
return "";
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "erro");
return "0";
}
}
public void atualizar(FuncionarioBeans funcionarioBeans) {
try {
Connection connection = new ConexaoUtil().getInstance().getConnection();
String sql = "UPDATE funcionários SET fun_nome = ?, fun_cargo = ? WHERE fun_cod = ?";
PreparedStatement statement = connection.prepareCall(sql);
statement.setString(1, funcionarioBeans.getnome());
statement.setString(2, funcionarioBeans.getcargo());
statement.setInt(3, funcionarioBeans.getcodigo());
statement.execute();
statement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
VLW!