英文:
java while (resultset.next()) does not return all data in the same column
问题
我需要你的帮助。我希望结果集能够获取"member_code"列中的所有数据,但目前只能获取一条数据。如何使用循环获取"member_code"中的所有数据?以下是我的代码:
public String membercode() throws SQLException{
String member = "";
String sql = "SELECT member_code FROM member";
java.sql.Connection conn = (Connection) koneksi.config();
java.sql.Statement stm = conn.createStatement();
java.sql.ResultSet res = stm.executeQuery(sql);
while (res.next()) {
member = res.getString("member_code");
}
return member;
}
英文:
i need your help. i want the resultset to fetch all data in member_code column, but it's only fetch 1 data. how to fetch all data in member_code using looping? here's my code:
public String membercode() throws SQLException{
String member="";
String sql = "SELECT member_code FROM member";
java.sql.Connection conn = (Connection) koneksi.config();
java.sql.Statement stm = conn.createStatement();
java.sql.ResultSet res = stm.executeQuery(sql);
while(res.next()) {
member = res.getString("member_code");
}
return member;
}
答案1
得分: 1
您需要组装并返回一个“list”。例如:
public List<String> membercode() throws SQLException{
List<String> members = new ArrayList<String>();
String sql = "SELECT member_code FROM member";
java.sql.Connection conn = (Connection) koneksi.config();
java.sql.Statement stm = conn.createStatement();
java.sql.ResultSet res = stm.executeQuery(sql);
while(res.next()) {
member = res.getString("member_code");
members.add(member);
}
return members;
}
英文:
You need to assemble and return a "list". For example:
public List<String> membercode() throws SQLException{
List<String> members = new ArrayList<String>();
String sql = "SELECT member_code FROM member";
java.sql.Connection conn = (Connection) koneksi.config();
java.sql.Statement stm = conn.createStatement();
java.sql.ResultSet res = stm.executeQuery(sql);
while(res.next()) {
member = res.getString("member_code");
members.add(member);
}
return members;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论