This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to store a check box in DataBase??

 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Here i am trying to retrieve check box values as

sqlquery = "insert into MY_TABLE(Interests)values(?)";

PreparedStatement.setArray(1,request.getParameterValues("selection"));

How will be the DataBase structure when we are inserting an Array of values.


Thanks in advance .
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question should be rephrased as "how to store an array in database". You don´t store checkboxes in DB.

Well, the answer is obvious: insert each value of the array in a new row in database. If all values are to be related to a single entity, then you can use a so-called chain table for this which you can later retrieve using a simple JOIN clause.

This question is in no way related to servlets by the way.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be frank i did not get you , i will Google on what you mentioned and ask you if needed.

Anyway thanks for responding to a non forum question .
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ravi, why you want to store all the check box vaue into one column in Database and also why you want to store as a array
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RaviNada Kiran wrote:To be frank i did not get you , i will Google on what you mentioned and ask you if needed.
I removed the typos and cleared it a bit up. Is all now clear? If you don´t understand certain terms or concepts, just ask about it the smart way.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64713
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on Servlets.

For more information, please read this.

This post has been moved to a more appropriate forum.

 
ujjwal soni
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Checkbox returns true false in 1 or 0 respectively. Store it as varchar or int32 in database.


Cheers!!!

Ujjwal B Soni
<baroda>
<gujarat>
<india>
<919998971048>
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ujjwal soni wrote:Hi,

Checkbox returns true false in 1 or 0 respectively. Store it as varchar or int32 in database.


so what you mean to say is that i can't store the value directly but in only in form of 1 or 0 .
so for this do i need to use SQL decode for inserting and retreving data ??
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhm, that fully depends on what items you're selecting.

When talking about a single item:
If it is just a boolean value (e.g. 'active', 'banned', 'show_email' etc) belonging to the same entity (e.g. User), then you can store it as BIT or TINYINT or any other smallest possible integer field (which is certainly not varchar or int32, that's a full waste of space) of the table representing the entity in question (the User).

If it is rather a complete entity (e.g. Country, Address, etc) which is supposed to be part of the parent entity (the User), then store it as FK of the table representing the entity in question. The entity itself should already be stored in a separate table.

When talking about multiple items:
Apply the same, but store the relations in a chain table. You should have one table for the parent entity (the User) and have another table for the items (e.g. VisitedCountries, Buddies, etc) and have a third table representing all relations between those two tables (a chain table) where in you store the related PK's of both tables as a new row.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4010
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd recommend TINYINT. You can use BIT but its a little more database-specific and may run into DB compatibilities down the road. TINYINT (or even INT) is good enough for what you need it for and doesn't take up much space.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic