データベースアプリケーションを開発しています。現在、H2組み込みデータベースと組み合わせてjava.sqlを使用しています。Don't Repeat Yourselfを発展させたいと思います。
そこで、再利用可能な Database Row クラスと Database Property クラスを次のように設定しました。プライベート T 値; プライベートブール識別子;
public DatabaseProperty(String PropertyName, T Value, boolean identifier) {
this.PropertyName = PropertyName;
this.Value = Value;
this.Identifier = identifier;
}
public String getPropertyName() {
return PropertyName;
}
public T getValue() {
return Value;
}
public void setValue(T Value) {
this.Value = Value;
}
public boolean isIdentifier() {
return Identifier;
}
}
そして... public class DatabaseRow { protected Connection DBConnection; 保護された文字列テーブル名; 保護された HashSet = 新しい HashSet<>();
public DatabaseRow() //With all the above variables. Apologies for being lazy to type ;)
//Here's the problem part
//I'm trying to automatically generate an SQL Statement
//to check that the row specified by primary unique keys (ex:- Username and Password Combination for Log In)
public boolean existsInTable(){
try {
String SQL = "SELECT * FROM "+TableName+" WHERE ";
boolean addAND = false;
for(DatabaseProperty d:Columns) {
if(d.isIdentifier()) {
SQL+=(addAND?"AND ":"")+d.getPropertyName()+" = ? ";
addAND = true;
}
}
PreparedStatement ps = getDBConnection().prepareStatement(SQL);
コードは続きます...
問題は、PeparedStatement クラスでパラメーターを設定するための汎用ベースのメソッドがないことです。代わりに setString(int index,String s) などがあります..これを克服するのを手伝ってください..PHP用のNotORMのようなオブジェクト指向のラッパーはありますか? このようなオプションでは、パフォーマンスとコーディングの容易さの間にトレードオフはありますか?