簡體   English   中英

如何將唯一數據從 excel 存儲到數據庫(使用 apache poi)?

[英]How to store unique data from excel to database(using apache poi)?

如果工作表中存在多個重復值(使用 apache poi),如何將 excel 中單元格中的唯一數據(例如電話號碼和 email Id 應該是唯一的)存儲到數據庫中?

下面是讀取 excel 數據並將其發送到 mysql 數據庫的代碼

公共 class ExcelToDatabase {

public static void main(String[] args) throws SQLException, IOException {
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName","root","root");
    Statement statement = con.createStatement();
    
    
    FileInputStream fis = new FileInputStream("/home/ist/Proj/ApachePOI/datafiles/TestData.xlsx");
    XSSFWorkbook workbook = new XSSFWorkbook(fis);
    XSSFSheet sheet = workbook.getSheetAt(0);
    
    int rows = sheet.getLastRowNum();
    
    for(int r=1; r<=rows; r++) {
        
        XSSFRow row = sheet.getRow(r);
        double EmpId = row.getCell(0).getNumericCellValue();
        String fName = row.getCell(1).getStringCellValue();
        String lName = row.getCell(2).getStringCellValue();
        int Age = (int) row.getCell(3).getNumericCellValue();
        String Email = row.getCell(4).getStringCellValue();
        String Phone = row.getCell(5).getStringCellValue();
        
        
        String sql = "INSERT INTO table_name values('"+EmpId+"','"+fName+"','"+lName+"', '"+Age+"','"+Email+"', '"+Phone+"')";
        statement.execute(sql);
        statement.execute("commit");
        
    }
    workbook.close();
    fis.close();
    con.close();
    
    System.out.println("data has been exported to the Database");
}
public static void main(String[] args) throws SQLException, IOException {
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName","root","root");
    Statement statement = con.createStatement();
    
    
    FileInputStream fis = new FileInputStream("/home/ist/Proj/ApachePOI/datafiles/TestData.xlsx");
    XSSFWorkbook workbook = new XSSFWorkbook(fis);
    XSSFSheet sheet = workbook.getSheetAt(0);
    
    int rows = sheet.getLastRowNum();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM