
excel
Javaimport Java.sql.*;public class excelToOracle { public static void mAIn(String[] args) { Connection conn = null; Statement stmt = null; try { // Establish Database connection String url = "jdbc:oracle:thin:@数据库地址:端口号:数据库名"; String user = "用户名"; String password = "密码"; conn = DriverManager.getconnection(url, user, password); // Open workbook object Workbook workbook = WorkbookFactory.create(new File("文件路径")); sheet = workbook.getSheet("Sheet1"); // Initialize variables int rowCount = sheet.getLastRowNum(); int colCount = sheet.getLastColumnNum(); for (int i = 0; i < rowCount; i++) { for (int j = 0; j < colCount; j++) { String cellValue = sheet.getcell(j, i).getStringCellValue(); // Insert into table String sql = "INSERT INTO table_name (column1, column2, column3) VALUES ('" + cellValue + "', '" + cellValue + "', '" + cellValue + "')"; stmt = conn.prepareStatement(sql); int count = stmt.executeUpdate(); if (count == -1) { System.out.println("更新数据失败"); } } } } catch (Exception e) { e.printStackTrace(); } finally { // Close resources try { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } }}
以上是一个简单的示例代码片段来演示如何使用JDBC将excel文件中的数据导入到Oracle数据库中。您需要根据实际情况修改连接参数(如用户名、密码、数据库地址等),并确保excel文件和Oracle表结构相匹配。Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号