Reading Empty Cell of Excel Sheet
I am reading contents of the excel sheet using the following code开发者_JAVA百科.
for (Row row : sheet) {
Cell firstCell = row.getCell(0);
Cell secondCell = row.getCell(1);
Cell thirdCell = row.getCell(2);
Cell fourthCell = row.getCell(3);
Cell fifthCell = row.getCell(4);
urlcnt=firstCell.getRichStringCellValue().getString();
srccnt=secondCell.getRichStringCellValue().getString();
contentType=thirdCell.getRichStringCellValue().getString();
verticle=fourthCell.getRichStringCellValue().getString();
timeFrame=fifthCell.getRichStringCellValue().getString();
}
I am assigning each cell of a particular row to a string as above.
PreparedStatement insertUrlStatement = con.prepareStatement("INSERT INTO urls_temp(url, source_name, is_active, is_periodic, Link_Type, New_Entry, verticle, periodic_timeframe, datentime) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)");
insertUrlStatement.setString(1, urlcnt);
insertUrlStatement.setString(2, srccnt);
insertUrlStatement.setInt(3, 1);
insertUrlStatement.setInt(4, 0);
insertUrlStatement.setString(5, contentType);
insertUrlStatement.setInt(6, 1);
insertUrlStatement.setString(7, verticle);
insertUrlStatement.setString(8, timeFrame);
insertUrlStatement.setString(9, datentime);
insertUrlStatement.executeUpdate();
Sometimes in the excel sheet, a cell may be left empty in a row by the user. During such cases this program is not inserting that whole row.
I want the empty cell to be saved as null in the table urls_temp.
How to attain the same? Please advise...
this might be an idea. Use a function which fills null in the statment if the supplied variable is empty. You might need to experiment with the (text = null) part but this could to the job. Sorry for my poor coding style. You will need such a function for any datatype.
function bindString(int column, String text) {
If (text == null) then
insertUrlStatement.setNull(column, STRING);
Else
insertUrlStatement.setString(column, text);
EndIf
}
精彩评论