我有这个功能,需要把它变成一个功能。唯一的区别是输入变量 sourceColumnValue 的类型。此变量可以是 String 或 Integer ,但函数的返回值必须始终为 Integer 。 我知道我需要使用泛型但不能这样做。
public Integer selectReturnInt(String tableName, String sourceColumnName, String sourceColumnValue, String targetColumnName) {
Integer returned = null;
String query = "SELECT "+targetColumnName+" FROM "+tableName+" WHERE "+sourceColumnName+"='"+sourceColumnValue+"' LIMIT 1";
try {
Connection connection = ConnectionManager.getInstance().open();
java.sql.Statement statement = connection.createStatement();
statement.execute(query.toString());
ResultSet rs = statement.getResultSet();
while(rs.next()){
returned = rs.getInt(targetColumnName);
}
rs.close();
statement.close();
ConnectionManager.getInstance().close(connection);
} catch (SQLException e) {
System.out.println("Заявката не може да бъде изпълнена!");
System.out.println(e);
}
return returned;
}
// SELECT (RETURN INTEGER)
public Integer selectIntReturnInt(String tableName, String sourceColumnName, Integer sourceColumnValue, String targetColumnName) {
Integer returned = null;
String query = "SELECT "+targetColumnName+" FROM "+tableName+" WHERE "+sourceColumnName+"='"+sourceColumnValue+"' LIMIT 1";
try {
Connection connection = ConnectionManager.getInstance().open();
java.sql.Statement statement = connection.createStatement();
statement.execute(query.toString());
ResultSet rs = statement.getResultSet();
while(rs.next()){
returned = rs.getInt(targetColumnName);
}
rs.close();
statement.close();
ConnectionManager.getInstance().close(connection);
} catch (SQLException e) {
System.out.println("Заявката не може да бъде изпълнена!");
System.out.println(e);
}
return returned;
}
答案 0 :(得分:1)
否您不需要使用泛型..当您支持的类型可以使用许多并且您事先不了解它们并且它们共享一些共同点时,应该使用泛型在他们中。
仅适用于两种类型泛型不是一个好选择。使用objects
可能是更好的选择。
可能我会说你甚至不需要合并这些函数,这就是多态性的用途。谨慎行事将使代码更易于阅读
答案 1 :(得分:0)
不需要使用泛型,您可以使用Object作为函数的变量类型:
public Integer selectIntReturnInt(String tableName, String sourceColumnName, Object sourceColumnValue, String targetColumnName) {
Integer returned = null;
String query = "SELECT "+targetColumnName+" FROM "+tableName+" WHERE "+sourceColumnName+"='"+sourceColumnValue.toString()+"' LIMIT 1";
try {
Connection connection = ConnectionManager.getInstance().open();
java.sql.Statement statement = connection.createStatement();
statement.execute(query.toString());
ResultSet rs = statement.getResultSet();
while(rs.next()){
returned = rs.getInt(targetColumnName);
}
rs.close();
statement.close();
ConnectionManager.getInstance().close(connection);
} catch (SQLException e) {
System.out.println("Заявката не може да бъде изпълнена!");
System.out.println(e);
}
return returned;
}
答案 2 :(得分:0)
您不需要使用泛型。只需将其声明为
public Integer selectReturnInt(String tableName,
String sourceColumnName,
Object sourceColumnValue,
String targetColumnName) {
...
}
答案 3 :(得分:0)
让第二种方法只调用第一种方法:
public Integer selectIntReturnInt(String tableName, String sourceColumnName, Integer sourceColumnValue, String targetColumnName) {
return selectReturnInt(tableName, sourceColumnName, sourceColumnValue.toString(), targetColumnName);
}
答案 4 :(得分:0)
对于爱美神,请不要使用动态SQL。你会得到注射漏洞。
你想把它分成(至少)三种方法。一个是大部分实现,一个是不同类型的。
另外值得注意的是:
final Resource resource = acquire(); try { ... } finally { resource.release(); }
或JDK7 try (final Resource resource = acquire()) { ... }
。printf
则很糟糕。