Einfügen von Daten Aus Excel in MySQL JDBC - POI-Beispiel-Programm

Möchte ich zum einfügen von Daten in MySQL-Datenbank-Tabelle aus Excel mit JDBC-manager mit Apache POI. Hier ist mein code:

TestApp.java

package testapp;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Row;

public class TestApp {

    public static void main(String[] args) throws Exception {

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/test","root","root");
            con.setAutoCommit(false);
            PreparedStatement pstm = null ;
            FileInputStream input = new FileInputStream("countrycode.xls");
            POIFSFileSystem fs = new POIFSFileSystem( input );
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            HSSFSheet sheet = wb.getSheetAt(0);
            Row row;
            for(int i=1; i<=sheet.getLastRowNum(); i++){
                row = sheet.getRow(i);
                String code = row.getCell(0).getStringCellValue();
                String desc = row.getCell(1).getStringCellValue();
                Date date = row.getCell(2).getDateCellValue();                
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String Date = sdf.format(date);
                String callcode = row.getCell(3).getStringCellValue();
                Boolean status = row.getCell(4).getBooleanCellValue();
                String currency = row.getCell(5).getStringCellValue();
                String sql = "INSERT INTO Ocountry (OCCODE, OCDESC, OCDT, OCCALlCODE, OCINACTIVE, OCUCODE) VALUES('"+code+"','"+desc+"','"+date+"','"+callcode+"','"+status+"','"+currency+"')";
                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) {
            e.printStackTrace();
        }
    }

}

Dies ist der Fehler, wenn ich die Datei ausführen:

Exception in thread "main" java.lang.NullPointerException
    at testapp.TestApp.main(TestApp.java:36)
Java Result: 1

Ist jemand in der Lage mir zu helfen, mein problem zu lösen?

Sie scheinen nicht zu checken, dass die Zelle einen Wert hat, bevor es zu Lesen. Was passiert, wenn Sie tun?
ich habe es zu überprüfen und es in der Lage, um den Wert zu speichern in die Datenbank. das problem ist gelöst.
Nur führen Sie Ihre Anwendung mit einem debugger und setzen Sie einen Haltepunkt in Zeile 36

InformationsquelleAutor susu | 2015-10-20

Schreibe einen Kommentar