[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Checking for empty rows

Posted on 2005-03-29
12
Medium Priority
?
3,224 Views
Last Modified: 2008-03-17
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?
0
Comment
Question by:komlaaa
  • 4
  • 4
  • 2
  • +2
12 Comments
 

Author Comment

by:komlaaa
ID: 13658181
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) );
            }
0
 
LVL 2

Assisted Solution

by:ThummalaRaghuveer
ThummalaRaghuveer earned 600 total points
ID: 13658221
Hi komlaaa,

The line if( val != null || val != "" )  try changing it to if(val != null && !val.equals(""))

Cheers!
Raghuveer
0
 
LVL 10

Expert Comment

by:kupra1
ID: 13658337
simply do this;
if( val != null || val != " " )  //notice the blank space

instead of
if( val != null || val != "" )
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 15

Expert Comment

by:aozarov
ID: 13658361
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
0
 
LVL 2

Expert Comment

by:ThummalaRaghuveer
ID: 13658374
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
0
 

Author Comment

by:komlaaa
ID: 13658688
>>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.
0
 

Author Comment

by:komlaaa
ID: 13658694
I have no clue why
0
 
LVL 15

Accepted Solution

by:
aozarov earned 800 total points
ID: 13658812
Change:
        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 );
to
        // Insert all the rows on CourseTable filled with data.
        for (int rowIndex=0; rowIndex< courseTable.getRowCount(); rowIndex++)
        {
            String classNum = (Object)courseTable.getValueAt(rowIndex, 0);
            if(classNum != null && classNum.trim().length() > 0) // just check the first val in JTable.
            {// Set the values
               
                //the JTable rows col start from 0.
                //save stNum value to update profTable.
                 pstmt.setString(1, classNum );

...
and then put your pstmt.executeUpdate() insteaad the this brackets
}
1
 
LVL 15

Expert Comment

by:aozarov
ID: 13658819
Sorry: also change:
String classNum = (Object)courseTable.getValueAt(rowIndex, 0);
to
String classNum = (String)courseTable.getValueAt(rowIndex, 0);
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13658824
Basically the imporant thing (after you fixed the "if" problem is that your executeUpdate should be inside that check.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13658943
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.
0
 

Author Comment

by:komlaaa
ID: 13659213
Thanks
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question