pstmt.setString(1, paper.getpName()) 拋出 ArrayIndexOutOfBoundsException,但 SQL 參數索引正確。
方法內定義了區域變數 Connection conn = ...,遮蔽了類成員 Connection conn。導致:
conn 用於查詢判重,查詢後關閉。conn(已關閉或為 null),PreparedStatement 參數綁定失敗拋出下標越界(實為連接無效的誤導性異常)。
// 移除區域變數聲明,直接使用類成員 conn
// 或統一命名:this.conn = getConnection();
// 更佳:使用 try-with-resources 管理連接生命週期
try (Connection conn = dataSource.getConnection();
PreparedStatement checkStmt = conn.prepareStatement(sqlCheck);
PreparedStatement insertStmt = conn.prepareStatement(sqlInsert)) {
// 判重
// 插入
}
addBatch() + executeBatch() 提升效能。