1
Hello! I’m new to Java, and I came across this problem:
Netbeans accuses that I am not respecting the SQL parameter limits, but are correct.
Part of the error in question:
Testando conexão com banco de dados...
jun 03, 2018 8:11:01 PM View.Autor.Autorview btnCadastrarActionPerformed
GRAVE: null
java.sql.SQLException: Parameter index out of range (3 > number of
parameters, which is 2).
My DAO class with method that accesses the bank:
public class AutorDAO {
private Connection c;
public AutorDAO(){
this.c = new Conexao().conectar();
}
public boolean verificaAutor(Autor autor, boolean result) throws SQLException{
PreparedStatement stmt = c.prepareStatement("SELECT nome, sobrenome FROM autor "
+ " WHERE nome=? AND sobrenome=?");
stmt.setString(2, autor.getNome());
stmt.setString(3, autor.getSobrenome());
//RS guarda o resultado da query
ResultSet rs = stmt.executeQuery();
if(!rs.next()){
result=false; //Retornou null, logo nao há registros
}
else
result=true; // se retorna true significa que há registro
stmt.execute();
stmt.close();
return result;}
public void insert(Autor autor){
String sql="INSERT INTO autor(idAutor, nome, sobrenome) values(?,?,?);";
try {
stmt.setInt (1, autor.getID());
stmt.setString(2, autor.getNome());
stmt.setString(3, autor.getSobrenome());
stmt.execute();
stmt.close();
} catch (SQLException e){
throw new RuntimeException(e);
}
}
}
My controller class calling DAO:
public class AutorControl {
boolean result;
public void CadastraAutor(String nome, String sobrenome) throws SQLException{
//Cria a model e DAO antes pra verificar se já existe
Autor autor = new Autor();
AutorDAO ad = new AutorDAO();
//Manda o model pra verificar se ja existe
//A result informa se já existe ou nao
ad.verificaAutor(autor, result);
//boolean resultado = new AutorDAO().verificaAutor();
//Se result for false, significa que não está cadastrado
if(result==false){
//Não há o autor, pode cadastrar
autor = new Autor(nome, sobrenome);
ad.insert(autor);
JOptionPane.showMessageDialog(null, "Autor cadastrado com sucesso!");
}
else
JOptionPane.showMessageDialog(null, "Autor já cadastrado!");
//Se retornar false significa que nao tem autor
//Então dessa mesma classe notifica pelo JOptionPane
//E já chama a tela de cadastro
}
}
EDIT1: Before, the DAO worked normally, accessed and inserted in the database. This error appeared after some modifications in the controller. If I go back to the controller as it was, the program runs and inserts into the seat normally.
Controller class before error:
public class AutorControl {
boolean result;
public void CadastraAutor(String nome, String sobrenome) throws SQLException{
//Cria a model e DAO antes pra verificar se já existe
Autor autor = new Autor();
AutorDAO ad = new AutorDAO();
autor = new Autor(nome, sobrenome);
ad.insert(autor);
}
}
Substitute
stmt.setString(2, autor.getNome());
andstmt.setString(3, autor.getSobrenome());
forstmt.setString(1, autor.getNome());
andstmt.setString(2, autor.getSobrenome());
, respectively.– Valdeir Psr
Man, THANK YOU SO MUCH. I was looking at this section and I thought it was very suspicious, but I thought this index was referring to the position of the attribute in the bank. Thank you very much!
– Weslley Fillipe
Is there any way I’d like to play the commentary? Enjoy, something like?
– Weslley Fillipe