从多列获取值

时间:2019-07-06 02:02:57

标签: java

我想通过以下方法从数据库中获取数据:

public List<String> getData(String sql, List<String> column) {
    this.driver();
    try {
        Connection conn = DriverManager.getConnection(this.url);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);

        List<String> result = new ArrayList<>();

        for (String i : column) {
            while (rs.next()) {
                result.add(rs.getString(String.valueOf(i)));
            }
        }
        return result;

    }
    catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

我正在使用以下方法接收数据:

public List<String> getAll() {
            Database database = new Database();
            List<String> columns = Arrays.asList("comment", "name");
            return database.getData("SELECT * FROM zt_productions.comments", columns);
}

在这种情况下,我想从列commentname中获取值,但奇怪的是,我只从列comment中获取值。怎么会?

0 个答案:

没有答案