我將更新MySQL資料庫中的一條記錄,我必須在代碼中使用注解和反射來更新一條記錄。
該方法將用戶的一個物件作為引數。每個物件都包含幾個欄位。 欄位上有注釋和反射。
我試圖用下面的代碼寫一個更新查詢。 我檢查了我的查詢很多遍,但沒有注意到我的錯誤。
我需要一個提示!
我需要一個提示。
public void updateRecord(Object object) {
String tableName = object.getClass().getDeclaredAnnotation(Table.class).name().equals(")
? object.getClass().getSimpleName() : object.getClass().getDeclaredAnnotation(Table.class) .name()。
try {
ArrayList<String> paramValue = new ArrayList<>()。
String query = "UPDATE" tableName " set" ;
Field[] fields = object.getClass().getDeclaredFields()。
for (Field field : fields) {
Annotation[] annotations = field.getDeclaredAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof Column && field.getAnnotation(Id.class) == null) {
String columnName = field.getAnnotation(Column.class) == null ? field.getName() : field.getAnnotation(Column.class).name() 。
query = columnName " = " " ? " " ,"。
Field objectFiled = object.getClass().getDeclaredField(field.getName())。
objectFiled.setAccessible(true)。
paramValue.add(String.valueOf(objectFiled.get(object))。
}
}
}
if (query.endWith(", ")) {
query = query.substring(0, query.length() - 1) 。
}
query = " Where ";
for (Field field : fields) {
Annotation[] annotations = field.getDeclaredAnnotations()。
for (Annotation annotation : annotations) {
Field objectFiled = object.getClass().getDeclaredField(field.getName() )。
objectFiled.setAccessible(true)。
if (annotation instanceof Id) {
String columnName = field.getAnnotation(Column.class) == null ? field.getName() : field.getAnnotation(Column.class).name() 。
query = columnName " = " objectFiled1.get(object)。
paramValue.add(String.valueOf(objectFiled1.get(object))。
}
}
}
System.out.println(query)。
preparedStatement = connection.prepareStatement(query);
for (int i=0; i < paramValue.size(); i ) {
preparedStatement.setString(i,paramValue.get(i))。
}
preparedStatement.executeUpdate()。
} catch (SQLException | IllegalAccessException | NoSuchFieldException ex) {
logger.error("User try to update an record from " tableName " but get an Exception")。
ex.printStackTrace()。
}
}
而這是一個例外:
UPDATE Person set firstname = ? , lastname = ? Where ID = 1 ?
java.sql.SQLException: Parameter index out of range (0 < 1 )。)
at
com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
)
atcom.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.ClientPreparedStatement.checkBounds(ClientPreparedStatement.java:1372)
at com.mysql.cj.jdbc.ClientPreparedStatement.getCoreParameterIndex(ClientPreparedStatement.java:1388)
at com.mysql.cj.jdbc.ClientPreparedStatement.setString(ClientPreparedStatement.java:1755)
at com.mapsa.orm.database.CRUDGenerator.updateRecord(CRUDGenerator.java:259)
at com.mapsa.orm.main.Execute.executeApplication(Execute.java:45)
at com.mapsa.orm.main.App.main(App.java:11)
uj5u.com熱心網友回復:
你需要傳遞ID欄位的值,但你實際上傳遞的是欄位的名稱,即ID。
嘗試以下代碼:
for(Field field: fields){
Annotation[] annotations = field.getDeclaredAnnotations()。
for (Annotation annotation : annotations) {
Field objectFiled = object.getClass().getDeclaredField(field.getName())。
objectFiled.setAccessible(true)。
if (annotation instanceof Id) {
String columnName = field.getAnnotation(Column.class) == null ?
field.getName() : field.getAnnotation(Column.class).name()。
// query = columnName; // <-- 這是不對的。
query = objectFiled.get(object); // try this。
}
}
}
另外,你需要在preparedStatement中設定引數值,你的代碼中缺少這個引數。
uj5u.com熱心網友回復:
改變你的if陳述句,代碼如下:
if (annotation instanceof Id) {
String columnName = field.getAnnotation(Column.class) == null ? field.getName() : field.getAnnotation(Column.class).name() 。
query = columnName " = " objectFiled.get(object)。
}
uj5u.com熱心網友回復:
問題出在這部分代碼中:
for(int i=0; i < paramValue.size(); i ){
preparedStatement.setString(i, paramValue.get(i))。
}
setString方法的索引從1開始,而不是從0開始--錯誤資訊中暗示了這一點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/306801.html
標籤:
