• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DB Field Length Validation Question

 
Simon O'Brien
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have just started my SCJD Coursework, I have been assigned a variety of Bodgitt and Scarper. :-)

In the Coursework Database Schema specification it states the length of each field in bytes, for example from B&S coursework:

Field Descriptive Name | Database Field Name | Field length
-----------------------|---------------------|--------------
Subcontractor Name | name | 32
City | location | 64

So in a setter for the Subcontractor name above would the length check be

OPTION 1:

if(name.length > 64 || name.length = 0)
{
throw IllegalArgumentException("error message etc")
}

or

OPTION 2:

if(name.getBytes("US-ASCII").length > 64 || name.length = 0)
{
throw IllegalArgumentException("error message etc")
}

I am quite certain that OPTION2 is the correct check however I thought I would see what the wise SCJD forum thinks?

Thanks if advance.

Simon
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'll use "ISO-8859-1".

Check this thread: Data File Format - US ASCII

Just document your decision.

bye,
Alex
[ November 23, 2007: Message edited by: Alex Belisle Turcot ]
 
mohamed sulibi
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

why you hard coded the lenght size , 64 , i think you are to save these value when you read it from the sechema section in the file , also i do what you do and throw the Illegal .... when any field size greater than expected .

OPTION 2:

if(name.getBytes("US-ASCII").length > 64 || name.length = 0)
{
throw IllegalArgumentException("error message etc")
}



m_darim.
SCJP, SCJD (in progress ... )
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just for the record, you should not use the charset JUST when checking the length.. anytime you convert bytes to String... (when reading and writing)

If you use the default one, then its fine, you use the default one all the time. Otherwise, you use different charset when reading/writing and checking the size..

bye,
Alex
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic