2

我在合并语句中使用绑定变量,它包含关联数组。什么是等效的 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();
4

1 回答 1

2

我认为您想使用一组不同的参数执行相同的语句。在 JDBC 中,它是使用批处理实现的

因此,您的结果代码将如下所示:

Connection conn = DriverManager.getConnection(jdbcUrl, connProps);
conn.setAutoCommit(false);
String sql = GetArrayMergeSQL();
PreparedStatement mergeStatement = conn.prepareStatement(sql);
int arrayCount = 3;
for (int i = 0; i < arrayCount; i++)
{
    mergeStatement.setString(1, "Victor" + (i + 1));
    mergeStatement.setString(2, "Alan" + (i + 1));
    mergeStatement.setString(3, "King" + (i + 1));
    mergeStatement.setInt(4, i + 1);
    mergeStatement.setString(5, "Alan" + (i + 1));
    mergeStatement.setString(6, "King" + (i + 1));
    mergeStatement.setString(7, "Scott" + (i + 1));
    mergeStatement.setString(8, "Victor" + (i + 1));
    mergeStatement.addBatch();
}
int[] count = mergeStatement.executeBatch();
conn.commit();

此外 JDBC 不支持命名参数 - 您必须将它们替换为 ? 标记并按位置引用它们:

 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
于 2015-07-15T11:26:39.697 回答