java 将数据从 Excel JDBC 插入 MySQL - POI 示例程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33248720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:25:24  来源:igfitidea点击:

Insert Data into MySQL From Excel JDBC - POI Example Program

javamysqlexceljdbcapache-poi

提问by susu

I would like to insert data into MySQL database table from Excel using JDBC manager with Apache POI. Here is my code:

我想使用带有 Apache POI 的 JDBC 管理器将数据从 Excel 插入到 MySQL 数据库表中。这是我的代码:

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();
        }
    }

}

This is the error given when I run the file:

这是我运行文件时给出的错误:

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

Is there anyone able to help me solve my problem?

有没有人能帮我解决我的问题?

回答by Steven Pyle

It looks like it doesn't like this line:

看起来它不喜欢这一行:

String Date = sdf.format(date);

String Date = sdf.format(date);

Are you sure that the variable datehas a non-null value at that point?

您确定该变量此时date具有非空值吗?

回答by Harshil Kulkarni

I used that code. It definitely works and I modified it as follows:

我用过那个代码。它确实有效,我对其进行了如下修改:

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
//import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.*;

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://localhost/tables", "root", "root");
        con.setAutoCommit(false);
        PreparedStatement pstm = null;
        FileInputStream input = new FileInputStream("C:\Users\Desktop\a1.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 add = row.getCell(1).getStringCellValue();

            int  contact = (int) row.getCell(2).getNumericCellValue();

            String email = row.getCell(3).getStringCellValue();

            String sql = "INSERT INTO employee (name, address, contactNo, email) VALUES('" + name + "','" + add + "'," + contact + ",'" + 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) {
     }
   }
}