我正在尝试将excel工作表数据插入mysql db。如果列中有数据,一切都会正常工作。但如果列为null,则程序停止执行,并且不会插入数据。
下面是我如何插入excel数据
测试PP。Java语言
public class TestApp {
public static void main(String[] args) throws Exception {
try {
Class forName = Class.forName("com.mysql.jdbc.Driver");
Connection con = null;
con =
DriverManager.getConnection("jdbc:mysql://192.168.1.13:3307/db1",
"sachin-LH", "s123");
con.setAutoCommit(false);
PreparedStatement pstm = null;
FileInputStream input = new
FileInputStream("C:\\Users\\lh\\Downloads\\Student Details(2009-
13).xls");
POIFSFileSystem fs = new POIFSFileSystem(input);
Workbook workbook;
workbook = WorkbookFactory.create(fs);
Sheet sheet = workbook.getSheetAt(0);
Row row;
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
row = (Row) sheet.getRow(i);
String name = row.getCell(0).getStringCellValue();
String email = row.getCell(1).getStringCellValue();
String sql = "INSERT INTO user
(firstName,email) VALUES('" + name +
"','"+email+"')";
pstm = (PreparedStatement) con.prepareStatement(sql);
pstm.execute();
System.out.println("Import rows " + i);
}
con.commit();
pstm.close();
con.close();
input.close();
System.out.println("Success import excel to mysql table");
} catch (IOException e) {
}
}
}
我的Excel工作表
Name email
test test@gmail.com
john ----
mike mike@gmail.com
第二行没有电子邮件,我收到空指针异常。如果特定列中不存在值,有人能告诉我如何插入空值吗。?