我想通过以下方法从数据库中获取数据:
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);
}
在这种情况下,我想从列comment
和name
中获取值,但奇怪的是,我只从列comment
中获取值。怎么会?