1
I have a database, and I have the user table and the address table. I need to validate somehow so that a user can have at most 5 addresses in the registry. I use the Postgresql database and it is an application with Servlet. Any suggestions on how I could perform this validation in the database or Servlet? I tried to do 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();
}
}
however do not know why the line where: result <= 5 is giving problem. The problem is related to the property I believe, but I don’t know what would be the correct property I could use for the code to work.
FK for User Table + Trigger. You can also validate in other application layers.
– Genos