java中,当使用`while (resultset.next())`时,并不会返回同一列中的所有数据。

huangapple go评论79阅读模式
英文:

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&lt;String&gt; membercode() throws SQLException{
    List&lt;String&gt; members = new ArrayList&lt;String&gt;();
    
    String sql = &quot;SELECT member_code FROM member&quot;;
        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(&quot;member_code&quot;);
             members.add(member);
             
        }
        return members;
}

huangapple
  • 本文由 发表于 2020年7月23日 23:25:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63057745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定