一般化メソッドを使用して、JavaでプレーンSQLクエリをパラメトリックSQLに変換したい
プロジェクトで使用した方法は以下のとおりです。これには多くの参照があります。
public Object executeQuery(String aQuery) {
Connection con = null;
Statement stmt = null;
ResultSet r = null;
try {
con = getConnection() ;
stmt = con.createStatement();
r = stmt.executeQuery(aQuery);
if (r.next()) {
//return extracted result;
}
return null;
} catch (SQLException e) {
throw new RuntimeException(
"Cannot execute query " + aQuery + " : " + e.getMessage());
} finally {
cleanup(r,stmt,con);
}
}
//=========
//calling above method
executeQuery("Update USER set user_id = 15 where user_name='test');
私はこれをJavaプリペアドステートメントを使用してパラメトリックSQLに変換することを考えていました。その後、メソッドの呼び出しは変更されませんが、次のようにクエリします
Update USER set user_id = 15 where user_name='test'
javaPreparedStatementと適切なパラメータを使用します
Update USER set user_id = ? where user_name=?
Springや他のフレームワークを使用するようにこれを行う方法はありますか?
前もって感謝します !