把Execl中的数据导入mysql数据库当中,一运行就空指针异常,错误提示应该找类中的 Statement,但是没太弄懂。
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
public class PoiRead {
public static void main(String[] args) throws Exception {
ArrayList<String> cars = new ArrayList<String>();
String PATH = "C:\\Users\\李晓彬\\Desktop\\汽车信息表.xlsx";
FileInputStream inputStream = new FileInputStream(PATH);
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
for (int i = 0; i < 6; i++) {
XSSFRow row = sheet.getRow(i);
int cellCount = row.getPhysicalNumberOfCells();
for (int cellNumber = 0; cellNumber < cellCount; cellNumber++) {
XSSFCell cell = row.getCell(cellNumber);
if (cell != null) {
CellType cellType = cell.getCellType();
// System.out.print(cell.getStringCellValue());
cars.add(cell.getStringCellValue());
}
}
}
Connection connection = null;
Statement statement;
ResultSet resultSet=null;
statement=connection.createStatement();
int n=0;
String sql=null;
//迭代List,批量入库。
for (String car:cars
) {
statement.executeBatch();
sql = "insert into car_information (car_id,car_name,car_people,car_room,car_day) values ('"+car+"')";
statement.executeUpdate(sql);
}
connection.commit();
}
}