Quando executo essa classe para fazer um insert em uma tabela chamada cargos que tem um unico campo chamado nome_cargo exibe esse erro
public class CadastrarCargo extends JPanel {
JLabel jl_msg;
JTextField tf_nome_cargo;
JButton bt_adicionar;
public CadastrarCargo() {
iniciarComponentes();
criarEventos();
}
private void iniciarComponentes() {
setLayout(null);
jl_msg = new JLabel("Nome do cargo");
tf_nome_cargo = new JTextField();
bt_adicionar = new JButton("Adicionar");
jl_msg.setBounds(150, 100, 300, 20);
tf_nome_cargo.setBounds(150, 120, 400, 30);
bt_adicionar.setBounds(250, 250, 200, 40);
add(jl_msg);
add(tf_nome_cargo);
add(bt_adicionar);
setVisible(true);
}
private void criarEventos() {
bt_adicionar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Cargo novoCargo = new Cargo();
novoCargo.setNome(tf_nome_cargo.getText());
sqlInserirCargo(novoCargo);
}
});
}
private void sqlInserirCargo(Cargo ncargo) {
if (tf_nome_cargo.getText().length() < 2) {
JOptionPane.showMessageDialog(null, “Nome digitado é muito curto”);
}
//conecção
Connection conexao;
//insrução sql
Statement instrucaoSQL;
ResultSet resultado;
try {
//conectando ao banco de dados
conexao = DriverManager.getConnection(Conexao.servidor, Conexao.usuario, Conexao.senha);
//criando a conexão
instrucaoSQL = conexao.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
instrucaoSQL.executeUpdate("INSERT INTO cargos (nome_cargo) VALUES ('" + ncargo.getNome() + "');");
JOptionPane.showMessageDialog(null, "Sucesso ao incluir cargo");
} catch (SQLException erro) {
JOptionPane.showMessageDialog(null, "ERRO AO ADICIONAR");
Logger.getLogger(CadastrarCargo.class.getName()).log(Level.SEVERE, null, erro);
}
}
}
ERROOO
run:
jun 09, 2019 12:52:52 PM sistema.telas.CadastrarCargo sqlInserirCargo
GRAVE: null
com.microsoft.sqlserver.jdbc.SQLServerException: Nome de objeto ‘cargos’ inválido.
** at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217)**
** at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1635)**
** at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:865)**
** at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:762)**
** at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:6276)**
** at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1794)**
** at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:184)**
** at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:159)**
** at com.microsoft.sqlserver.jdbc.SQLServerStatement.execute(SQLServerStatement.java:735)**
** at sistema.telas.CadastrarCargo.sqlInserirCargo(CadastrarCargo.java:85)**
** at sistema.telas.CadastrarCargo.access$000(CadastrarCargo.java:25)**
** at sistema.telas.CadastrarCargo$1.actionPerformed(CadastrarCargo.java:62)**
** 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:6533)**
** at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)**
** at java.awt.Component.processEvent(Component.java:6298)**
** at java.awt.Container.processEvent(Container.java:2237)**
** at java.awt.Component.dispatchEventImpl(Component.java:4889)**
** at java.awt.Container.dispatchEventImpl(Container.java:2295)**
** at java.awt.Component.dispatchEvent(Component.java:4711)**
** at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)**
** at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4526)**
** at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467)**
** at java.awt.Container.dispatchEventImpl(Container.java:2281)**
** at java.awt.Window.dispatchEventImpl(Window.java:2746)**
** at java.awt.Component.dispatchEvent(Component.java:4711)**
** at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)**
** 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:80)**
** at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)**
** at java.awt.EventQueue$4.run(EventQueue.java:731)**
** at java.awt.EventQueue$4.run(EventQueue.java:729)**
** at java.security.AccessController.doPrivileged(Native Method)**
** at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)**
** at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)**
** 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)**