Prevent database insertion with String " (with space in name)

Asked

Viewed 299 times

-1

My problem is this::

I have a Jtextfield that will receive the name of my entity, will pass to the controller, which will create the bean and finally, call the DAO to insert into the bank. Simple thing a basic CRUD.

The table field in the database is NOT NULL, because it wouldn’t make sense to be blank.

Thinking about: Make It Simple! I did the following in the method that will be responsible for the events in Jdialog, I will have the empty spaces of the name start and use a Joptionpane if Jtextfield is empty.

However we will also be evaluated by the elegance of the code, better ways to solve the problems, for example using Pattern. Following this logic in the controller’s method, responsible for instantiating the DAO and inserting in the database a Boolean return only, to warn the method that called whether or not the insertion was successful. In this case, the method will check if the entity name is null, and if there is an equal name already registered in the database, instantiating an Arraylist with all registered names and using a Foreach for verification, if the name already exists returns false.

But, as I said, we will be judged by the elegance of the code, so imagine making an exception and treating it back if the method returns false. Seeking more information I read:

How to best treat exceptions in Java?
It is good practice to make an exception in such cases?
Exception vs Runtimeexception, when using one or the other?

And some other things more for my better understanding that cleared up a little, but, I still have the doubt: This way is correct or would be more, say "elegant", treat as an exception?

Codes:

Save button method:

public void setupEvents() {
    btnSave.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            tfNameNewDiscipline.getText().trim();
            ControllerDiscipline controller;

            if(tfNameNewDiscipline.getText().isEmpty())
                JOptionPane.showMessageDialog(null, "Digite um nome para a Disciplina", "Erro no cadastro da Disciplina", JOptionPane.ERROR_MESSAGE);
            else  {
                controller = new ControllerDiscipline();
                if(controller.addDiscipline(tfNameNewDiscipline.getText()) == false)
                    JOptionPane.showMessageDialog(null, "Disciplina já existe", "Erro no cadastro da Disciplina", JOptionPane.ERROR_MESSAGE);
            }
        }
    });

Method of the controller class you add to the database:

public boolean addDiscipline(String name) {

    DisciplineDAO dao = new DisciplineDAO();
    Discipline entity = new Discipline();

    entity.setName(name);

    if (entity.getName().isEmpty()) {
        return false;
    }

    ArrayList<Discipline> disciplines = new ArrayList<>();
    disciplines.addAll(dao.searchData());

    for(Discipline discipline : disciplines) {
        if(entity.getName().equals(discipline.getName()))
            return false;
    }

    dao.insert(entity);
    return true;
}
  • Have you searched for InputVerifier? It inserts a Runtime limitation into the field, which if empty, won’t even let actionperformed be executed. Have a look in this question, there’s an example there.

  • so soon after asking the question, I thought, however she will help me with the Jtextfield but, to validate the insertion not... but, I think I will use it, because I believe that got better the implementation of the example you cited.

  • Is the goal to prevent empty fields from being inserted into the bank, triggering exceptions by field restriction? Filtering just before the user saves, you are already avoiding having to worry about these kind of exceptions, having to fill your Try/catchs codes. :)

  • This way then you think it is already more than enough... there is no need to create an exception to this case?

  • Yes, the inputverifier will not let the form be submitted until the field is greater than zero, this of course, assuming you apply this condition in the verify.

1 answer

-1


Friendly talk!

About the exceptions: I haven’t found anywhere the launch or capture of Exceptions.. Is it really implemented? Even though you’re being judged for the elegance of the code, I think exaggeration treat validations of view with Exceptions. I find it much more valid to treat your business rule validations (Business or Controller layer: depends on how you are implementing right hehe) with Exceptions than View.

On the validation of " ": I usually use the class StringUtils package org.apache.commons.lang3 to make that kind of validation. She has (if I’m not mistaken), a method called isNotNullOrEmpty() that works that is a beauty! :)

I hope I’ve helped!

  • No I didn’t implement I was just thinking about doing that, but I was wondering if it’s too much or not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.