Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

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();
0
komlaaa
Asked:
komlaaa
  • 3
  • 2
2 Solutions
 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now