I have a database, and I have the user table and the address table. I need to validate in some way so that a user can have a maximum of 5 addresses in the registry. I use the PostgreSQL database and treats an application with Servlet. Any suggestions on how I could perform this validation in the database or Servlet? I tried doing this validation in the application:
public void register (address, user user) throws ClassNotFoundException, SQLException {
Connection con = FabricaConexao.getConexao(); //conexão com o banco de dados
PreparedStatement comand = con.prepareStatement("select count(*) from Endereco where usuario_id=?");
comand.setInt(1,1);
ResultSet result = comand.executeQuery(); //result é o resultado do select certo?
if (result>=5){ //entao aqui eu tento realizar: Se o resultado do select for menor que 5, entao eu vou cadastrar, porem esta dando erro aqui.
PreparedStatement comando = con.prepareStatement("insert into endereco (cep,numero,bairro,rua,estado,cidade,complemento,usuario_id) values (?,?,?,?,?,?,?,?)");
comando.setString(1,endereco.getCep());
comando.setInt(2,endereco.getNumero());
comando.setString(3,endereco.getBairro());
comando.setString(4,endereco.getRua());
comando.setString(5,endereco.getEstado());
comando.setString(6,endereco.getCidade());
comando.setString(7,endereco.getComplemento());
comando.setInt(8,usuario.getId());
comando.execute();
con.close();
}else{
con.close();
}
}
But I do not know why the line where: result