Link to home
Start Free TrialLog in
Avatar of Vanavah Edwards
Vanavah Edwards

asked on

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

Avatar of for_yan
for_yan
Flag of United States of America image

Are you talking about chekboxes or comboboxes (or even maybe radioboxes)?
Checkboxes have methos isSelected() which should return boolean and not getSelectedItem()
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
So for real JcheckBox you'll have

+ (checkBox.isSelected() ? 1 : 0) +
Assuming that selected value corresponds to 1 and non-selected coresponds to zero
Avatar of Vanavah Edwards
Vanavah Edwards

ASKER

for updates instead of insert would this be correct where accessDO is the JCheckBox
accessDO='"+(accessDO.isSelected() ? 1 : 0)+"'
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
I am using BOOLEAN in the database
Which is the database?

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Otherwise
checkBox.isSelected()
Returns booleann so you can try
smth like that

"AccessDO = " + accessDO.isSelected() + " ...
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.
Well, at least for me it is dedfinitely more natural, as I use Oracle which uses 0 and 1 numbers for booleans.
The checkboxes worked on insert I am now trying to populate fields from the db.  i am almost done.
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"));
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

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
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 "".  


>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

>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
I mean the only way to test for an empty JTextField is to use ""
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

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
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.
If nothing is entered, i then prompt the user to enter something.

well, this is how I nomally do it;
if(jTextFiled.getText().trim().length() == 0)
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

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);
....

}






}




}

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 + "'";


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

Open in new window

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


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
Your query most probably reurns nothing as you don't have rows belonging to that user
Okay i don't need the while loop, I understand.  But I was formerly using rsData.next() instead
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
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

It works when I cut off
WHERE CELL_DBUSERNAME= '"+cell_dbUsername+"' "

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

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]'
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

Check boxes work perfect.  I am closing.  Anymore on the JTextField.
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
Thanks very much for your help again.  I have made alot of progress thank to you.
You are always welcome.