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();
請詳細說明您對「不起作用」的使用。 (錯誤等) –
我不能使用索引作爲參數,因爲綁定變量不是按順序和不同的更新和插入(綁定變量的數量) –
啊,我混淆了你的問題的方向性。你想* Java *代碼?如果是這樣,請發佈您的(最佳)嘗試Java實現。這將會走很長的路。 –