Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Array Length/Size

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am doing a simple (but not for me) JDBC implementation with java. I have this customer class which puts customers into an array:



I am trying to create a new customer in the event there is no customer match. I need to create a new ID. I think the best way to do this is to reference the length of the array and +1 but I am not sure how to do that. I have the following code:



I think I MIGHT be on the right track with:



But I don't feel null is the right value and I don't know what value should go there.


 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally we let the DB determine the IDs for us, such as with a sequence.

If you're going to do it yourself, then taking the highest ID present and adding 1 is a valid approach, but it can be a hassle to keep track of it in all situations, especially if your app is multithreaded. You're really better off letting the DB manage it.
 
Meg Berg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just an assignment for a database class and so I realize the implementation may not be a real world scenario.

How do I create the ID within the confines of needing to do it in the java code? Is (null) the right value on the .toArray?

Thanks!

Meg
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Meg Berg wrote:This is just an assignment for a database class and so I realize the implementation may not be a real world scenario.

How do I create the ID within the confines of needing to do it in the java code? Is (null) the right value on the .toArray?

Thanks!

Meg


it's not right, why use null?

the parameter means:
the array into which the elements of this collection are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.

if the specified array is null, there would be NullPointerException.
 
Meg Berg
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if I seem dense, it isn't intentional, I assure you. In my OP, I agreed that I didn't think null was the right value, but I am totally lost as to what I SHOULD put in there or if that will even help me do what I need it to do.

Meg
 
Sheriff
Posts: 67398
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In order to call toArray() you already need a collection of customer instances. And if you have that, you already know the count and don't need the array at all.
 
I don't like that guy. The tiny ad agrees with me.
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic