我用谷歌搜索了一遍,但找不到一个合适的工作示例,最后我转换了 this对于我的情况,导入 mysql ,代码如下:
import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import java.util.*;
import java.sql.*;
public class Import {
public static void main(String[] args) throws Exception{
/* Create Connection objects */
// Class.forName ("oracle.jdbc.OracleDriver");
Properties connectionProps = new Properties();
connectionProps.put("user", "myUSERNAME");
connectionProps.put("password", "myPASSWORD");
Connection conn = DriverManager.getConnection("jdbc:mysql://"
+ "serverIP" + ":" + "3306" + "/" + "myDB",
connectionProps);
PreparedStatement sql_statement = null;
String jdbc_insert_sql = "INSERT INTO myTABLE"
+ "(LastName, FirstName) VALUES"
+ "(?,?)";
sql_statement = conn.prepareStatement(jdbc_insert_sql);
/* We should now load excel objects and loop through the worksheet data */
FileInputStream input_document = new FileInputStream(new File("insert_old.xls"));
/* Load workbook */
HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document);
/* Load worksheet */
HSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0);
// we loop through and insert data
Iterator<Row> rowIterator = my_worksheet.iterator();
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING: //handle string columns
sql_statement.setString(1, cell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC: //handle double data
sql_statement.setDouble(2,cell.getNumericCellValue() );
break;
}
}
// Add the row to a batch processing - standard batch processing example
sql_statement.addBatch();
}
//We are now ready to perform a bulk batch insert
//our Excel has only 4 records, you should set this suitably
int[] totalRecords = new int[4];
try {
totalRecords = sql_statement.executeBatch();
} catch(BatchUpdateException e) {
//you should handle exception for failed records here
totalRecords = e.getUpdateCounts();
}
System.out.println ("Total records inserted : " + totalRecords.length);
/* Close input stream */
input_document.close();
/* Close prepared statement */
sql_statement.close();
/* COMMIT transaction */
// conn.commit();
/* Close connection */
conn.close();
}
}
这是输出: “插入的记录总数:4”
Excel 文件有 4 行 2 列,填充了简单文本。不幸的是,我查看数据库,没有导入任何内容,当我使这个简单的插入工作时,我想稍后修改它以插入更多图像文件,文件路径将存储在 xls 文件中,这个版本也是我需要采用的 xls xlsx 也是如此。如果您能帮助我找到代码中的错误或为 xls 和 xlsx 提供更好的工作示例,我将很高兴。我正在使用 Apache POI。
最佳答案
你可以简单地这样做: 假设该文件位于 C:\filename.xlsx
Connection con = //...Complete connection initialization
PreparedStatement pre = con.prepareStatement("load data infile 'C:\\filename.xlsx' into table tablename");
pre.executeUpdate();
一旦避免以下错误,此代码将完美运行: 1. 确保文件位于目录的根目录而不是其他目录(这可能会导致异常) 2. 确保用单引号引用路径
关于java - 如何将 Excel 中的行插入到 mysql 数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28324280/