File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes How to store a check box in DataBase?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to store a check box in DataBase??" Watch "How to store a check box in DataBase??" New topic
Author

How to store a check box in DataBase??

RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
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 .


If you want something you never had do something which you had never done
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Jan 30, 2009
Posts: 528
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Jan 10, 2002
Posts: 61653
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
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>


Cheers!!!
Ujjwal B Soni <baroda, gujarat, india> <+919909981973>
"Helping hands are better than praying lips......"
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
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

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Oct 23, 2005
Posts: 3716
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to store a check box in DataBase??