java - 如何将唯一数据从 excel 存储到数据库(使用 apache poi)?
问题描述
如果工作表中存在多个重复值(使用apache poi),如何将excel中单元格中的唯一数据(例如电话号码和电子邮件ID应该是唯一的)存储到数据库中?
下面是读取excel数据并将其发送到mysql数据库的代码
公共类 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();
}
推荐阅读
- java - New Relic中如何查看Kafka Consumer函数响应时间
- java - 无法使用 Jetty 服务器线程池最大大小限制并发请求,为什么?
- javascript - 从 HTML 中的另一个 JavaScript 文件运行 JavaScript 文件
- c# - 为什么将 [ ] 添加到 json 字符串有效,为什么它不能更早地工作
- javascript - 如何对使用 ViewChild 访问的元素使用 js dom 操作
- php - 未使用移动数据从 aws 服务器下载数据
- amazon-ec2 - 没有 Autoscaling 的 ECS 集群
- sql - SQL加入大表性能问题
- java - 如果您最终可以通过公共方法访问私有数据,封装如何保护它?
- javascript - Rails 使用 JavaScript 自动完成