How to insert checkbox boolean into a table

I have a few checkboxes in my program.  I want to store their boolean variables in a table.  I tried the attached code way but it doesn't work.  How is it really done?
'"+accessDO.getSelectedObjects().getActionCommand()+"'

Open in new window

Vanavah EdwardsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
Are you talking about chekboxes or comboboxes (or even maybe radioboxes)?
Checkboxes have methos isSelected() which should return boolean and not getSelectedItem()
0
for_yanCommented:
In the database where you want to store - you should look up if it supports special boolean values; some databases just use 0 or 1 instead of boolean
0
for_yanCommented:
So for real JcheckBox you'll have

+ (checkBox.isSelected() ? 1 : 0) +
Assuming that selected value corresponds to 1 and non-selected coresponds to zero
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vanavah EdwardsAuthor Commented:
for updates instead of insert would this be correct where accessDO is the JCheckBox
accessDO='"+(accessDO.isSelected() ? 1 : 0)+"'
0
for_yanCommented:
Yes for update it should work, howvere you may consider if you want to use varchar type in database or the number filed - if it is the number filed then you don't need to add sungle quote
0
Vanavah EdwardsAuthor Commented:
I am using BOOLEAN in the database
0
for_yanCommented:
Which is the database?

0
for_yanCommented:
I see some controversies about whether sqllite has booleans:

http://stackoverflow.com/questions/580668/how-do-i-use-a-boolean-field-in-a-where-clause-in-sqlite

I think it is simpler to use number filed - then the expression I showed above with
CheckBox.isSelected() ? 1 :0
can be used as it converts boolean to number

 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:
Otherwise
checkBox.isSelected()
Returns booleann so you can try
smth like that

"AccessDO = " + accessDO.isSelected() + " ...
0
Vanavah EdwardsAuthor Commented:
You are the expert.  I will follow your instructions.  I will use numbers.  I will change the fields in the tables in the database to integers.
0
for_yanCommented:
Well, at least for me it is dedfinitely more natural, as I use Oracle which uses 0 and 1 numbers for booleans.
0
Vanavah EdwardsAuthor Commented:
The checkboxes worked on insert I am now trying to populate fields from the db.  i am almost done.
0
Vanavah EdwardsAuthor Commented:
I am doing the reverse getting the numeric values from the db table.  Should I retrieve the numeric values like this
accessDO.setSelected(rsData.getBoolean("accessDO"));
0
for_yanCommented:

I'm not sure it will work this way.

You rather do:

int num = rsData.getInt("accessDO");
if(num == 0) accessDO.setSelected(false);
elseaccessDO.setSelected(true);
0
Vanavah EdwardsAuthor Commented:
Earlier you had said to use .EQUALS() with components.  And this is what I did here

System.out.println(cell_dbUsername.getText().equals(null));

I got the same problem again.  These JTextField here cell_dbUsername is null and I am testing for null.  I just hit tab in the field and when I print the result it says false.  I am doing this in a focus event method.  Please help me to understand this .EQUALS once and for all.  This is a simple thing and I cannot believe this can give me so much trouble that I have to write you about it.  i thought I was long pass this.
0
for_yanCommented:

I'm not sure what you are doing, but these are some points to keep in mind:

if(cell_dbUsername == null) {
// then you should not try to get any text from it, so you may check that cell_dbUsername is not null from the verty beginning
//cell_dbUsername will be null when it was not constructted; even when it is empty, but have been constructed it will not produce null

}else if (cell_dbUsername.getText().equals("some_other_text")){
//do somthing
}

if cell_dbUsername is not null, then cell_dbUsername.getText() will actuially never be null
This could be an empty string - "", but it would never be null
so no need to check
cell_dbUsername.getText().equals(null);

The same applies to any JTextField - getText() alwyas returns not null
for JTextField
0
Vanavah EdwardsAuthor Commented:
You are right.  When I use double quotes "" it works.  This means with JTextFields if it always return not null I can only test for null with "".  
0
for_yanCommented:


>Earlier you had said to use .EQUALS() with components

I made general statement, that ANY OBJECTS cannot be compared
using ==. the == can be used only with primitives; all non-primitive objects should be compared
with equals() method

having said that - it ususally does not make sense compare JTextFields or JPaswordFields
or any elements with each other at all

In most cases youi want to read the conrtents of
JTextField's or JPasswordField's
and compare the String's,
you don't need to comparea the JPasswordFields themselves

It may lead to confusing results - JPassworFields
will have the saem contents - and that is waht really matteers,
but the they will not be equal - as they have different location on the screen, different size, or whatevere - you don't want to compare the JPasswordFields
you want to compare the struings which they conatin

0
for_yanCommented:
>This means with JTextFields if it always return not null
This is true.

> I can only test for null with "".  
This is not understantadable what you mean
0
Vanavah EdwardsAuthor Commented:
I mean the only way to test for an empty JTextField is to use ""
0
for_yanCommented:
I don't see why you need to test for an empty TextFiled but you can do it this way:

if(jTextFiled.gettext().trim().length() == 0) then it is empty
0
for_yanCommented:

I don't see why you need to test for an empty TextFiled but you can do it this way:

if(jTextFiled.getText().trim().length() == 0) then it is empty
0
Vanavah EdwardsAuthor Commented:
A test for a an empty field is very important in some cases where fields are supposed to be not null like a username in my case.  This is the only way to test if the user has entered anything.
0
Vanavah EdwardsAuthor Commented:
If nothing is entered, i then prompt the user to enter something.
0
for_yanCommented:

well, this is how I nomally do it;
if(jTextFiled.getText().trim().length() == 0)
0
Vanavah EdwardsAuthor Commented:
I have not gotten far.  I have been trying to retrive the variables in the db and I keep getting resultSet Closed at line 6.  What am I doing wrong?
Statement st = conn.createStatement();
			    String sqlString = "SELECT * FROM CELL_USERS for " +
			    		"WHERE CELL_DBUSERNAME='"+cell_dbUsername+"' ";
				ResultSet rsData = st.executeQuery(sqlString);
				rsData.next();
				if (rsData.getString("cell_dbUsername")==cell_dbUsername.getText()) {
			    	accessDO.setSelected(rsData.getBoolean("accessDO"));
			    	accessSCU.setSelected(rsData.getBoolean("accessSCU"));
			    	accessSCO.setSelected(rsData.getBoolean("accessSCO"));
			    	accessSDB.setSelected(rsData.getBoolean("accessSDB"));
			    	accessSUA.setSelected(rsData.getBoolean("accessSUA"));
			    	accessRDOS.setSelected(rsData.getBoolean("accessRDOS"));

Open in new window

0
for_yanCommented:
why you are doing all opposite to waht we were talking about?

You don't use == for objects - once and for all.
If you stored them as numbers, why are you doing rsData.getBoolean... ?

then normal way to iterate through result set is something like that:

while(rsData.next()) {

String user = rsData.getString("cell_dbUsername");
if(user.equals(cell_dbUsername.getText().trim()){

int num = rsData.getInt("accessDO"));
if(num == 0)accessDO.setSelected(false);
else accessDO.setSelected(true);

 num = rsData.getInt("accessSCU"));
if(num == 0)accessSCU.setSelected(false);
else accessSCU.setSelected(true);
....

}






}




}
0
for_yanCommented:

In addition this seems strange SQL.
Waht for is there "for"  after CELL_USERS ?


String sqlString = "SELECT * FROM CELL_USERS for " +
                                  "WHERE CELL_DBUSERNAME='"+cell_dbUsername+"' ";
Should be something like that:

String sqlString = "select * from cell_users where  CELL_DBUSERNAME='"+cell_dbUsername + "'";

0
Vanavah EdwardsAuthor Commented:

while (rsData.next()) {
       String user = rsData.getString("cell_dbUsername");
       \System.out.println(user);
       if (user.equals(cell_dbUsername.getText().trim())) {

Open in new window

0
Vanavah EdwardsAuthor Commented:
Sorry, i did what you said but it wouldn't work.  I tried printing the user name in the loop but nothing prints.
while (rsData.next()) {
       String user = rsData.getString("cell_dbUsername");
       System.out.println(user);
       if (user.equals(cell_dbUsername.getText().trim())) {

Open in new window

0
for_yanCommented:

i'm not sure why you need this:
 if (user.equals(cell_dbUsername.getText().trim()))

You are selecting only those which correspond to that user based on the where clause of the query
0
for_yanCommented:
Your query most probably reurns nothing as you don't have rows belonging to that user
0
Vanavah EdwardsAuthor Commented:
Okay i don't need the while loop, I understand.  But I was formerly using rsData.next() instead
0
for_yanCommented:
No, you always need the while(rsdata.next()){} loop
This is just a standard for any traversal of resultset - no snesne even thing about it - always use it

You don't need  if (user.equals(cell_dbUsername.getText().trim())) {...
because you are seleceting only tiose which staisfy this condition
0
Vanavah EdwardsAuthor Commented:
I have check that tablle there is the same user name I am typing.  I check all spelling of all the objects.  I check the table and everything seems accurate.
Statement st = conn.createStatement();
			    String sqlString = "SELECT * FROM CELL_USERS WHERE CELL_DBUSERNAME= '"+cell_dbUsername+"' ";
				ResultSet rsData = st.executeQuery(sqlString);
				int noi=0 ;
				while (rsData.next()) {
					noi++;
			    	int num = rsData.getInt("accessDO");
			    	if(num == 0) accessDO.setSelected(false);
			    	else accessDO.setSelected(true);
				    	 
			    	num = rsData.getInt("accessSCU");
			    	if(num == 0) accessSCU.setSelected(false);
			    	else accessSCU.setSelected(true);

			    	num = rsData.getInt("accessSCO");
			    	if(num == 0) accessSCO.setSelected(false);
			    	else accessSCO.setSelected(true);

			    	num = rsData.getInt("accessSDB");
			    	if(num == 0) accessSDB.setSelected(false);
			    	else accessSDB.setSelected(true);

			    	num = rsData.getInt("accessSUA");
			    	if(num == 0) accessSUA.setSelected(false);
			    	else accessSUA.setSelected(true);

			    	num = rsData.getInt("accessRDOS");
			    	if(num == 0) accessRDOS.setSelected(false);
			    	else accessRDOS.setSelected(true);

Open in new window

0
Vanavah EdwardsAuthor Commented:
It works when I cut off
WHERE CELL_DBUSERNAME= '"+cell_dbUsername+"' "
0
for_yanCommented:

Add these two printouts - one of the sqlString, another within the resultyset traversal loop:
Statement st = conn.createStatement();
			    String sqlString = "SELECT * FROM CELL_USERS WHERE CELL_DBUSERNAME= '"+cell_dbUsername+"' ";
                                  System.out.println(sqlString);

				ResultSet rsData = st.executeQuery(sqlString);
				int noi=0 ;
				while (rsData.next()) {
                                                           System.out.println("noi: " + noi);
					noi++;
			    	int num = rsData.getInt("accessDO");
			    	if(num == 0) accessDO.setSelected(false);
			    	else accessDO.setSelected(true);
				    	 
			    	num = rsData.getInt("accessSCU");
			    	if(num == 0) accessSCU.setSelected(false);
			    	else accessSCU.setSelected(true);

			    	num = rsData.getInt("accessSCO");
			    	if(num == 0) accessSCO.setSelected(false);
			    	else accessSCO.setSelected(true);

			    	num = rsData.getInt("accessSDB");
			    	if(num == 0) accessSDB.setSelected(false);
			    	else accessSDB.setSelected(true);

			    	num = rsData.getInt("accessSUA");
			    	if(num == 0) accessSUA.setSelected(false);
			    	else accessSUA.setSelected(true);

			    	num = rsData.getInt("accessRDOS");
			    	if(num == 0) accessRDOS.setSelected(false);
			    	else accessRDOS.setSelected(true);

Open in new window

0
Vanavah EdwardsAuthor Commented:
Printouts
SELECT * FROM CELL_USERS WHERE CELL_DBUSERNAME='javax.swing.JTextField[,180,18,170x20,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@aae86e,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]'
0
Vanavah EdwardsAuthor Commented:
I got the problem solved.  It works only when I get the text from the JTextField  

String musername = new String(cell_dbUsername.getText()); 
String sqlString = "SELECT * FROM CELL_USERS WHERE CELL_DBUSERNAME='"+musername+"'";

Open in new window

0
Vanavah EdwardsAuthor Commented:
Check boxes work perfect.  I am closing.  Anymore on the JTextField.
0
for_yanCommented:
Of course, it makes sense.
It didn't occur to me that cell_dbUsername is not a tring but the lememnt itslef.
Of course you should first use getText() and then feed the text string to the query
0
Vanavah EdwardsAuthor Commented:
Thanks very much for your help again.  I have made alot of progress thank to you.
0
for_yanCommented:
You are always welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.