?
Solved

check for empty row

Posted on 2005-03-28
5
Medium Priority
?
279 Views
Last Modified: 2010-03-31
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
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
  • 3
  • 2
5 Comments
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 180 total points
ID: 13643449
>> 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
 
LVL 15

Accepted Solution

by:
aozarov earned 320 total points
ID: 13643655
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 13643687
>> 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
 
LVL 15

Expert Comment

by:aozarov
ID: 13643948
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 13643977
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

771 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