-1

データベースアプリケーションを開発しています。現在、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のようなオブジェクト指向のラッパーはありますか? このようなオプションでは、パフォーマンスとコーディングの容易さの間にトレードオフはありますか?

4

1 に答える 1

2

これを使用してみてください:

ps.setObject(index, object);

index が null でないすべての場合に機能するはずです。あなたの場合は問題ないと思います。オブジェクトがnullの場合、タイプを設定する必要があります

ps.setObject(index, null, type);

パラメータ メタデータ オブジェクトから取得できる型:

ParameterMetaData meta=ps.getParameterMetaData();
int type = meta.getParameterType(index);
于 2013-05-09T18:13:39.907 に答える