• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems avoiding the insertion of empty text fields into the sqlite database in Java  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I have a form in my java inventory software that permits a shop manager to create new sales agents accounts.

It has 6 fields : firstNameField, lastNameField, dateOfBirthField, phoneNumberField, agentIDField, and agentPasswordField.
It also have a button called Add

This form stores its values in an SQLite database table called: sales_agents
Here's its schema:



My goal is, when the manager clicks the Add button -- which is linked to a method called agentCreateBtnActionPerformed(), the following code should check that no field has been left blank,
before inserting anything to the database.
In case, there is an empty field, it prints out this message:

Please you are required to enter something in every empty field to carry out this operation!

in
a JOptionPane.

Here's the code:



My Problem.
1) The above code keeps inserting empty strings when there are empty fields. And I've also realized that, when I insert something in the  agentPasswordField, and don't insert anything in the
agentIDField, and vice versa, it inserts both non-empty field values and empty field values to the database without popping up an error message.
Whereas, failing to insert into the agentIDField, and agentPasswordField generates an error message in JOptionPane that says:

A primary key constraint failed (UNIQUE constraint failed: sales_agents.userID, sales_agents.userPassword)



2) I added a check constraint to the userPasswordField thinking it would generate an SQLException, but it still did not work.

I've made a lot of research on this issue online but couldn't find something that helped.

Please, any help will be welcome.
regards
 
Sheriff
Posts: 4424
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think carefully about the logic in this line.  Will you get to the "then" part only if all fields are filled?

Also, there is a method in the String object called isEmpty().  I would use that instead of equals("").
 
Knute Snortum
Sheriff
Posts: 4424
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this literally what is in the CREATE TABLE definition?  It looks like the check is "not equal to one space".  Is that what you want?
 
Ranch Foreman
Posts: 3136
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute's spotted the immediate issue, but I have a question.
Why is your DOB column declared as TEXT rather than a DATE?
It looks like the source of the date of birth is already a date (maybe?)?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!