2012-11-27 54 views
2

我在合併語句中使用綁定變量,並且它包含關聯數組。什麼是等效的Java代碼。我嘗試使用PreparedStatement和callable語句,但它們不適用於數組。C#在Java中用於將關聯數組添加到命令參數

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語句

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(); 
+0

請詳細說明您對「不起作用」的使用。 (錯誤等) –

+0

我不能使用索引作爲參數,因爲綁定變量不是按順序和不同的更新和插入(綁定變量的數量) –

+0

啊,我混淆了你的問題的方向性。你想* Java *代碼?如果是這樣,請發佈您的(最佳)嘗試Java實現。這將會走很長的路。 –

回答

1

我認爲你要與一組不同的參數來執行相同的語句。在JDBC它使用batching

所以,你的結果代碼將看起來像這樣實現的:

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