我正在开发一个数据库应用程序。目前我正在使用结合 H2 嵌入式数据库的 java.sql。我想开发“不要重复自己”的方式。
所以我设置了一个可重用的Database Row类和Database Property类如下: public class DatabaseProperty { private String PropertyName; 私人 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; 受保护的字符串表名;protected HashSet = new 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?使用这些选项在性能和编码易用性之间是否有任何权衡?