This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

How to go about this mySQL implementation

 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have a program I made for summer camps created in java swing that assigns campers to cabins, prints lists and medical reports.  

I'm working on a web application to assign activities, and over the next couple years, I hope to have it be an all out registration and camp management service.

Right now, there is no database linked, but the objects are just saved to a file.  

So, I created an int variable for each class (Camper, Counselor, Cabin, CampInformation).  However, I don't want to assign a number to it until it gets sent over to the database after the user saves it and links to it.  

So, what I wanted to do, is when they link to the database, it enters the objects not found into the tables, and then it assigns a camper number, cabin number, etc.  After it assigns a primary key, I want it to send back that key to the object in an ArrayList.  


So, a couple ideas I had since int cannot be null are...

I would either just make all int variables 0 from the start, and loop through the ArrayList when saving the objects.  For all objects where the int equals 0, it will store them in the database, and then it will take that key from the database, and assign the int to it.  If it is not 0, it verifies the information in the database is the same, and if not, it will change each individual variable that has changed from the previous time.


The other idea I had was changing it to Integer and putting them in as null values, but doing essentially the same thing.   Would both ideas work?  Would one be better than the other?  Or is it that I'm very confusing here and nobody knows what the hell I'm talking about?
 
Sheriff
Posts: 6108
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see why both ideas wouldn't work.  Using int, I might have the "empty signal" be -1 and then make all valid IDs nonnegative.

Some people frown on using a null to mark an empty value.  The worry is that it could introduce NullPointerErrors.

There is a third way that might be a little too much for the situation, but it would be to use Optional<Integer>
 
Knute Snortum
Sheriff
Posts: 6108
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Option<Integer> would look something like this:
 
Marshal
Posts: 65021
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or . . . int id = myOptional.orElse(−999999999); or similar.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!