我在合并语句中使用绑定变量,它包含关联数组。什么是等效的 Java 代码。我尝试使用 PreparedStatement 和 callable 语句,但它们不适用于数组。
comm.Parameters.Add(new OracleParameter(":student_id", OracleDbType.Varchar2, arrayCount, studentId, System.Data.ParameterDirection.Input));
comm.Parameters.Add(new OracleParameter(":first_name", OracleDbType.Varchar2, arrayCount, firstName, System.Data.ParameterDirection.Input));
comm.Parameters.Add(new OracleParameter(":last_name", OracleDbType.Varchar2, arrayCount, lastName, System.Data.ParameterDirection.Input));
comm.Parameters.Add(new OracleParameter(":created_by", OracleDbType.Varchar2, arrayCount, createdBy, System.Data.ParameterDirection.Input));
comm.Parameters.Add(new OracleParameter(":modified_by", OracleDbType.Varchar2, arrayCount, modifiedBy, System.Data.ParameterDirection.Input));
if(conn.State != ConnectionState.Open)
conn.Open();
comm.ArrayBindCount = arrayCount;
comm.BindByName = true;
这是我的合并声明
MERGE INTO STUDENT USING DUAL ON(student_id = :student_id)
WHEN MATCHED THEN
UPDATE SET modified_by = :modified_by,
first_name = :first_name,
last_name = :last_name
WHEN NOT MATCHED THEN
INSERT (student_id, first_name, last_name, created_by, modified_by)
VALUES (:student_id, :first_name,:last_name,:created_by,:modified_by)
LOG ERRORS INTO ERR$_STUDENT ('MERGE') REJECT LIMIT UNLIMITED
示例 Java 代码
conn = DriverManager.getConnection(jdbcUrl,connProps);
conn.setAutoCommit(false);
stmt = conn.createStatement();
int arrayCount = 3;
String[] studentId = new String[arrayCount];
String[] firstName = new String[arrayCount];
String[] lastName = new String[arrayCount];
String[] createdBy = new String[arrayCount];
String[] modifiedBy = new String[arrayCount];
for (int i = 0; i < arrayCount; i++)
{
studentId[i] = String.valueOf(i + 1);
firstName[i] = "Alan" + (i + 1);
lastName[i] = "King" + (i + 1);
createdBy[i] = "Scott" + (i + 1);
modifiedBy[i] = "Victor" + (i + 1);
}
String sql = GetArrayMergeSQL();
PreparedStatement mergeStatement = conn.prepareStatement(sql);
NamedParameterStatement p = new NamedParameterStatement(conn,sql);
CallableStatement merge = (CallableStatement) conn.createStatement();
merge.setNString(":student_id", studentId);
int count = stmt.executeUpdate(sql);
conn.commit();