从sql查询方法返回泛型类型

时间:2016-02-21 05:33:57

标签: java sql generics resultset

我正在尝试从sql查询方法返回泛型类型,但我仍然坚持将数据库中的值赋给泛型类型:

public static <T extends Comparable<T>> T selectQuery(Connection conn, String sql, T[] parameters) {
        T value = null;
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            List<T> list = Arrays.asList(parameters);
            for (T param : list) {
                if (param instanceof String) {
                    ps.setString(list.indexOf(param), (String) param);
                } else if (param instanceof Integer) {
                    ps.setInt(list.indexOf(param), (Integer) param);
                } else if (param instanceof Double) {
                    ps.setDouble(list.indexOf(param), (Double) param);
                }
            }
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                 //AM STUCK RIGHT HERE************
            }
            Conn.closeConn(conn);
        } catch (Exception asd) {
            System.out.println(asd.getMessage());
        }
        return value;
    }

请帮我提出应对这一挑战的建议。

1 个答案:

答案 0 :(得分:1)

test :: (a -> Bool) -> [a] -> [a]
test _ [] = []
test func (x:xs)
    | some code here
    | some code here 
    where test _ (x:xs) = reverse xs ++ [x]

有关Types in sql

的进一步参考