Link to home
Start Free TrialLog in
Avatar of komlaaa
komlaaa

asked on

Checking for empty rows

Hi,
Here is my GUI:http://people.clarkson.edu/~havenssm/gui6.doc
And this is how i am checking for empty rows(that i would not like to be entered in database)

 for (int rowIndex=0; rowIndex< courseTable.getRowCount(); rowIndex++)
        {
            Object val = (Object)courseTable.getValueAt(rowIndex, 0);
            if( val != null || val != "" ) // just check the first val in JTable.

but all the rows including EMPTY ONES are being inserted into Course Table.
How can i avoid that?
Avatar of komlaaa
komlaaa

ASKER

More detailed code:
 PreparedStatement pstmt = accessCon.prepareStatement(addCourseSql);
        String classNum = "";
        // Insert all the rows on CourseTable filled with data.
        for (int rowIndex=0; rowIndex< courseTable.getRowCount(); rowIndex++)
        {
            Object val = (Object)courseTable.getValueAt(rowIndex, 0);
            if( val != null || val != "" ) // just check the first val in JTable.
            {// Set the values
               
                //the JTable rows col start from 0.
                //save stNum value to update profTable.
                classNum = (String)courseTable.getValueAt(rowIndex, 0);                
                pstmt.setString(1, classNum );
                pstmt.setString(2, (String)courseTable.getValueAt(rowIndex, 1) );
                pstmt.setString(3, (String)courseTable.getValueAt(rowIndex, 2) );
                pstmt.setInt(4, profID );
                pstmt.setString(5, (String)courseTable.getValueAt(rowIndex, 3) );
                pstmt.setString(6, (String)courseTable.getValueAt(rowIndex, 4) );
            }
SOLUTION
Avatar of ThummalaRaghuveer
ThummalaRaghuveer

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
simply do this;
if( val != null || val != " " )  //notice the blank space

instead of
if( val != null || val != "" )
I will take ThummalaRaghuveer instead of kupra1 (checking for val != "" will work only if val is an intern String).
But then you might want to be stricker and check it like this:
if (value != null && val.trim().length() > 0)

That is in case you to consider empty spaces string as empty
kupra1,

it should not be done the way mentoned.  For example String val = ""  and I use the condition mentioned above, it obviously fails. because val does not contain null and the conditional statement evaluates to true so no further checking takes place. Also one should not compare Strings using == or !=.

Raghuveer
Avatar of komlaaa

ASKER

>>The line if( val != null || val != "" )  try changing it to if(val != null && !val.equals(""))
this is working but if i enter 6 course, the same course is being inserted the 4 additional time
(number of last four remainning empty rows
Normaly by putting this *if( val != null || val != "" ) * only the row filled with data should be inserted.
Avatar of komlaaa

ASKER

I have no clue why
ASKER CERTIFIED 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
Sorry: also change:
String classNum = (Object)courseTable.getValueAt(rowIndex, 0);
to
String classNum = (String)courseTable.getValueAt(rowIndex, 0);
Basically the imporant thing (after you fixed the "if" problem is that your executeUpdate should be inside that check.
Avatar of Mayank S
I think that this is same as another of your questions posted, where I had mentioned that the executeUpdate () should also be called only if the condition is satisfied. You are calling executeUpdate () regardless of the if () statement's condition, outside it.
Avatar of komlaaa

ASKER

Thanks