Java/SpringBoot连接Oracle 8i及批量插入相关技术问题咨询
连接Oracle 8i数据库的正确方案
一、驱动版本选择问题
你使用的ojdbc8是为Oracle 12c及以上版本设计的,完全不兼容Oracle 8i,这是导致连接报错(数组越界)的核心原因。Oracle 8i需要匹配ojdbc14驱动(适配JDK 1.4及以上,SpringBoot基于JDK8运行时也可兼容)。
1. 驱动获取与安装
ojdbc14不在Maven中央仓库,需手动获取后安装到本地Maven仓库:
mvn install:install-file -Dfile=ojdbc14.jar -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=9.2.0.8 -Dpackaging=jar
(版本号需与你实际下载的驱动一致,Oracle 8i对应的驱动版本通常为9.2.x系列)
2. 添加Maven依赖
在pom.xml中引入驱动:
<dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>9.2.0.8</version> </dependency>
3. SpringBoot数据源配置
Oracle 8i仅支持传统URL格式,配置示例(application.properties):
spring.datasource.url=jdbc:oracle:thin:@<主机地址>:<端口>:<SID> spring.datasource.username=你的用户名 spring.datasource.password=你的密码 spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver # HikariCP适配配置(可选) spring.datasource.hikari.maximum-pool-size=5 spring.datasource.hikari.connection-timeout=30000
二、Oracle 8i的批量插入支持
Oracle 8i支持批量插入,可通过原生JDBC或Spring JdbcTemplate实现:
1. 原生JDBC实现
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.sql.DataSource; public class BatchInsertDemo { private final DataSource dataSource; public BatchInsertDemo(DataSource dataSource) { this.dataSource = dataSource; } public void executeBatchInsert() throws SQLException { String insertSql = "INSERT INTO your_table (col1, col2) VALUES (?, ?)"; try (Connection conn = dataSource.getConnection(); PreparedStatement pstmt = conn.prepareStatement(insertSql)) { // 批量添加数据 for (int i = 0; i < 200; i++) { pstmt.setString(1, "data_" + i); pstmt.setInt(2, i); pstmt.addBatch(); // 每50条执行一次批处理,避免内存溢出 if ((i + 1) % 50 == 0) { pstmt.executeBatch(); pstmt.clearBatch(); } } // 执行剩余未提交的批处理 pstmt.executeBatch(); } } }
2. Spring JdbcTemplate实现
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter; import java.util.List; public class JdbcTemplateBatchDemo { private final JdbcTemplate jdbcTemplate; public JdbcTemplateBatchDemo(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void batchInsert(List<YourEntity> entityList) { String sql = "INSERT INTO your_table (col1, col2) VALUES (?, ?)"; jdbcTemplate.batchUpdate(sql, entityList, entityList.size(), (ParameterizedPreparedStatementSetter<YourEntity>) (pstmt, entity) -> { pstmt.setString(1, entity.getCol1()); pstmt.setInt(2, entity.getCol2()); }); } }
注:批量插入时建议分批次提交,避免一次性加载过多数据导致内存占用过高。
内容的提问来源于stack exchange,提问作者Alok Kumar
相关产品推荐
相关产品推荐

