?
Solved

Checking for empty rows

Posted on 2005-03-29
12
Medium Priority
?
2,994 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

800 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