check for empty row

this code basically insert JTable rows in a database, before processing any row i would like to make sure that the row is not empty. What the most effiecient way to do this?

 String addCourseSql = "INSERT INTO Course(" +
                    "[Class Nbr], Subject,Catalog,ProfessorID, Section, Descr)" +
                    " VALUES(?, ?, ?, ?, ?, ? )";
         
        PreparedStatement pstmt = accessCon.prepareStatement(addCourseSql);
   
        // Insert all the rows on CourseTable filled with data.
        for (int rowIndex=0; rowIndex< courseTable.getRowCount(); rowIndex++) {
            // Set the value
            for( int colIndex = 1; colIndex < courseTable.getColumnCount(); colIndex++ )
            {
                if( 1 == colIndex) pstmt.setString(colIndex, clNum);
                if(4 == colIndex) pstmt.setInt( colIndex, profID );
                pstmt.setObject(colIndex, (Object)courseTable.getValueAt(rowIndex, colIndex) );
            }
   
            // Insert the row
            pstmt.executeUpdate();
komlaaaAsked:
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.

Mayank SAssociate Director - Product EngineeringCommented:
>> pstmt.setObject(colIndex, (Object)courseTable.getValueAt(rowIndex, colIndex) );

This will be executed regardless of whether the first two if statements run or not.

>> (Object)courseTable.getValueAt(rowIndex, colIndex)

The (Object) cast is not needed.

>> i would like to make sure that the row is not empty

You mean you want to check the value at that row/ column is not null? Just check it as:

Object val = courseTable.getValueAt ( rowIndex, colIndex ) ;

if ( val != null )
  pstmt.setObject ( colIndex, val ) ;
0
aozarovCommented:
Are you sure you want to do:
 if( 1 == colIndex) pstmt.setString(colIndex, clNum);
                if(4 == colIndex) pstmt.setInt( colIndex, profID );
                pstmt.setObject(colIndex, (Object)courseTable.getValueAt(rowIndex, colIndex) );

instead of
 if( 1 == colIndex) pstmt.setString(colIndex, clNum);
 else if(4 == colIndex) pstmt.setInt( colIndex, profID );
 else pstmt.setObject(colIndex, (Object)courseTable.getValueAt(rowIndex, colIndex) );
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
Mayank SAssociate Director - Product EngineeringCommented:
>> else pstmt.setObject(

Yeah, should be that way. That's essentially what I meant by: >> regardless of whether the first two if statements run or not.
0
aozarovCommented:
I know :-) but why to hint ;-)
Also, what the reason for:
if ( val != null )
  pstmt.setObject ( colIndex, val ) ;

instead of just: pstmt.setObject ( colIndex, val ) ;
After all this is the formal way to set the column to NULL (if val is null)
0
Mayank SAssociate Director - Product EngineeringCommented:
Yeah, that's why I asked him what he meant by: >> i would like to make sure that the row is not empty

If a null value is not to be inserted, then the pstmt.executeUpdate () should not be called and the value also need not be set :)
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
Java

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.