通用选择java中的方法/推理方法

时间:2017-11-17 00:37:15

标签: java generics methods inference

我对泛型没有任何经验,但我设置了一些代码。 我的目标是实现searchData(ResultSet rs),以便它选择与data对应的正确方法,该方法是给定的类/ databaseField名称。

示例:

对于data ="中"我想致电searchMedium(rs)

对于data ="客户"我想致电searchCustomer(rs)

对于data ="贷款"我想致电searchLoan(rs)

我不想用if或switch case来实现它,因为这对于许多这些方法来说会变得很大。 我希望你能理解我的意图,并有可能实现它。 这是我的代码:

public class test3 {
public static void main(String[] args) {
    test3 t = new test3();
    ArrayList<?> list = t.search("medium");
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }
}

public <T> ArrayList<T> search(String data) {
    ArrayList<T> resultList = new ArrayList<T>();
    Connection c;
    Statement stmt;
    String sql = "Error occurred before executing SQL statement";
    try {
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:library.sqlite");
        c.setAutoCommit(false);
        stmt = c.createStatement();
        sql = "SELECT * FROM " + data + ";";
        ResultSet rs = stmt.executeQuery(sql);

        resultList = searchData(rs, data);
        rs.close();
        stmt.close();
        c.close();
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage() + "\n" + sql + "\nSQL query for " + data
                + " failed");
    }
    return resultList;
}

private <T> ArrayList<T> searchData(ResultSet rs, String data) {
    ArrayList list = null;
    // TODO ???
    return list;
}

private ArrayList<Loan> searchLoan(ResultSet rs) throws SQLException {
    ArrayList<Loan> resultList = new ArrayList<Loan>();
    Loan match;
    while (rs.next()) {
        int loanNumber = rs.getInt("NUMBER");
        String mediumId = rs.getString("MEDIUMCODE");
        int customerNumber = rs.getInt("CUSTOMERNUMBER");
        String loanDate = rs.getString("LOANDATE");
        String targetReturndate = rs.getString("TARGETRETURNDATE");
        String returnDate = rs.getString("RETURNDATE");
        int extensions = rs.getInt("EXTENSIONS");
        match = new Loan(loanNumber, mediumId, customerNumber, targetReturndate, loanDate, returnDate, extensions);
        resultList.add(match);
    }
    return resultList;
}

private ArrayList<Medium> searchMedium(ResultSet rs) throws SQLException {
    ArrayList<Medium> resultList = new ArrayList<Medium>();
    Medium match;
    while (rs.next()) {
        String number = String.valueOf(rs.getInt("NUMBER"));
        String id = rs.getString("ID");
        String isbn = rs.getString("ISBN");
        String title = rs.getString("TITLE");
        String authors = rs.getString("AUTHORS");
        String type = rs.getString("TYPE");
        String category = rs.getString("CATEGORY");
        String publishingYear = String.valueOf(rs.getInt("PUBLISHINGYEAR"));
        String publishingMonth = rs.getString("PUBLISHINGMONTH");
        String timestamp = rs.getString("TIMESTAMP");
        String loanerNumber = String.valueOf(rs.getInt("LOANERNUMBER"));
        match = new Medium(number, id, isbn, title, type, authors, category, publishingYear, publishingMonth,
                timestamp, loanerNumber);
        resultList.add(match);
    }
    return resultList;
}

private ArrayList<Customer> searchCustomer(ResultSet rs) throws SQLException {
    ArrayList<Customer> resultList = new ArrayList<Customer>();
    Customer match;
    while (rs.next()) {
        String number = String.valueOf(rs.getInt("NUMBER"));
        String id = rs.getString("ID");
        String lastname = rs.getString("LASTNAME");
        String firstname = rs.getString("FIRSTNAME");
        String birthdate = rs.getString("BIRTHDATE");
        String zipcode = String.valueOf(rs.getInt("ZIPCODE"));
        String domicile = rs.getString("DOMICILE");
        String street = rs.getString("STREET");
        String housenumber = String.valueOf(rs.getInt("HOUSENUMBER"));
        String notification = rs.getString("NOTIFICATION");
        String notificationTimestamp = rs.getString("NOTIFICATIONTIMESTAMP");
        match = new Customer(number, id, lastname, firstname, birthdate, zipcode, domicile, street, housenumber,
                notification, notificationTimestamp);
        resultList.add(match);
    }
    return resultList;
}

}

可能我的方法非常糟糕,所以我会非常乐意提供任何帮助/建议。

非常感谢

1 个答案:

答案 0 :(得分:0)

根据我的理解,在这里你不能使用Generics动态调用方法。为此你必须使用方法重载。如果你愿意放弃泛型,那么可以使用以下方式。

private RestultSet getResultSet(String tblName){
    Connection c;
    Statement stmt;
    String sql = "Error occurred before executing SQL statement";
    try {
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:library.sqlite");
        c.setAutoCommit(false);
        stmt = c.createStatement();
        sql = "SELECT * FROM " + tblName+ ";";
        ResultSet rs = stmt.executeQuery(sql);

        return rs;
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage() + "\n" + sql + "\nSQL query for " + data
            + " failed");
    }
}

public ArrayList<Customer> search(Customer c) {
    ArrayList<Customer> resultList = new ArrayList<>();
    ResultSet rs = getRestultSet(c.getSimpleName());

    Customer match;
    while (rs.next()) {
        String number = String.valueOf(rs.getInt("NUMBER"));
        String id = rs.getString("ID");
        String lastname = rs.getString("LASTNAME");
        String firstname = rs.getString("FIRSTNAME");
        String birthdate = rs.getString("BIRTHDATE");
        String zipcode = String.valueOf(rs.getInt("ZIPCODE"));
        String domicile = rs.getString("DOMICILE");
        String street = rs.getString("STREET");
        String housenumber = String.valueOf(rs.getInt("HOUSENUMBER"));
        String notification = rs.getString("NOTIFICATION");
        String notificationTimestamp = rs.getString("NOTIFICATIONTIMESTAMP");
        match = new Customer(number, id, lastname, firstname, birthdate,     zipcode, domicile, street, housenumber,
            notification, notificationTimestamp);
        resultList.add(match);
    }

    rs.close();
    return resultList;
}

public ArrayList<Medium> search(Medium m) {
    ArrayList<Medium> resultList = new ArrayList<>();
    ResultSet rs = getRestultSet(m.getSimpleName());

    Medium match;
    while (rs.next()) {
        String number = String.valueOf(rs.getInt("NUMBER"));
        String id = rs.getString("ID");
        String isbn = rs.getString("ISBN");
        String title = rs.getString("TITLE");
        String authors = rs.getString("AUTHORS");
        String type = rs.getString("TYPE");
        String category = rs.getString("CATEGORY");
        String publishingYear = String.valueOf(rs.getInt("PUBLISHINGYEAR"));
        String publishingMonth = rs.getString("PUBLISHINGMONTH");
        String timestamp = rs.getString("TIMESTAMP");
        String loanerNumber = String.valueOf(rs.getInt("LOANERNUMBER"));
        match = new Medium(number, id, isbn, title, type, authors, category, publishingYear, publishingMonth,
            timestamp, loanerNumber);
        resultList.add(match);
    }

    rs.close();
    return resultList;
}

同样,您也可以为贷款提供搜索方法
主要方法如下

public static void main(String[] args) {
    test3 t = new test3();
    ArrayList<?> list = t.search(new Medium());
    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }
}

希望这会奏效......谢谢......