Quando clico no botão “Salvar aparece a seguinte mensagem de erro”
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: com.microsoft.sqlserver.jdbc.SQLServerException: O valor não está definido para o número de parâmetro 4.
at entidades.UsuarioDAO.adiciona(UsuarioDAO.java:44)
at erp.cadastro.jBsalvarActionPerformed(cadastro.java:187)
at erp.cadastro.access$300(cadastro.java:22)
at erp.cadastro$4.actionPerformed(cadastro.java:87)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: O valor não está definido para o número de parâmetro 4.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.buildParamTypeDefinitions(SQLServerPreparedStatement.java:260)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.buildPreparedStrings(SQLServerPreparedStatement.java:219)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doPrepExec(SQLServerPreparedStatement.java:612)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:400)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.execute(SQLServerPreparedStatement.java:332)
at entidades.UsuarioDAO.adiciona(UsuarioDAO.java:40)
... 39 more
Classe ConnectionFactory
[quote]package entidades;
/**
*
-
@author dhenrique
*/
import java.sql.Connection; // classe para tratamento de exceções
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
public Connection getConnection() {
try {
return DriverManager.getConnection(“jdbc:sqlserver://localhost:1433;databaseName=erp”,“sa”,“Suporte*12”);
}
catch(SQLException excecao) {
throw new RuntimeException(excecao);
}
}
}[/quote]
Classe Pessoa
[code]package entidades;
// Generated 13/12/2012 10:16:40 by Hibernate Tools 3.2.1.GA
/**
-
Pessoa generated by hbm2java
*/
public class Pessoa implements java.io.Serializable {private int id;
private String nome;
private String salario;
private String outraRenda;public Pessoa() {
}public Pessoa(int id) {
this.id = id;
}
public Pessoa(int id, String nome, String salario, String outraRenda) {
this.id = id;
this.nome = nome;
this.salario = salario;
this.outraRenda = outraRenda;
}public int getId() {
return this.id;
}public void setId(int id) {
this.id = id;
}
public String getNome() {
return this.nome;
}public void setNome(String nome) {
this.nome = nome;
}
public String getSalario() {
return this.salario;
}public void setSalario(String salario) {
this.salario = salario;
}
public String getOutraRenda() {
return this.outraRenda;
}public void setOutraRenda(String outraRenda) {
this.outraRenda = outraRenda;
}
}
[/code]
Classe UsuarioDAO
[code]/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package entidades;
/**
*
-
@author dhenrique
*/
import entidades.ConnectionFactory;
import entidades.Pessoa;
import java.sql.*;
import java.sql.PreparedStatement;
public class UsuarioDAO {
private Connection connection;
Long id;
String nome;
String cpf;
String email;
String telefone;
public UsuarioDAO(){
this.connection = new ConnectionFactory().getConnection();
}
public void adiciona(Pessoa pessoa){
String sql = "INSERT INTO usuario(nome,cpf,email,telefone) VALUES(?,?,?,?)";
try {
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, pessoa.getNome());
stmt.setString(2, pessoa.getSalario());
stmt.setString(3, pessoa.getOutraRenda());
stmt.execute();
}
} catch (SQLException u) {
throw new RuntimeException(u);
}
}
}
[/code]