• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic