The problem of using generics in a function

I have these functions and need to make it one function. The only difference is the type of the input variable sourceColumnValue. This variable can be String or Integer, but the return value of the function should always be Integer. I know that I need to use Generics, but I can not do this.

    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;
}
+3
source share
5 answers

No, you do not need to use generics for this. generic should be used when your supported types may be for many, and you don't know about them before, and they have something in common in them.

For just two types, Generics is not a good choice. Use objectsmay be a better choice.

, , , .

+1

generic, 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;
}
0

.

public Integer selectReturnInt(String tableName, 
      String sourceColumnName, 
      Object sourceColumnValue, 
      String targetColumnName) {
      ...
 }
0

:

public Integer selectIntReturnInt(String tableName, String sourceColumnName, Integer sourceColumnValue, String targetColumnName) {
    return selectReturnInt(tableName, sourceColumnName, sourceColumnValue.toString(), targetColumnName);
}
0

$, , SQL. .

( ) . .

:

  • final Resource resource = acquire(); try { ... } finally { resource.release(); } JDK7 try (final Resource resource = acquire()) { ... }.
  • - .
  • , printf .
  • You should probably return a value only if it has only one set of results and an exception is thrown.
0
source

Source: https://habr.com/ru/post/1795064/


All Articles