我在mySql数据库中有2条记录,但无法从名为origin的列中检索2个值。我只能在第一行中检索值,而不能在第二行中检索。我的代码在以下方法中:

public String[] sequences(){
    String[] origins=new String[2];


     Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/geneBank";
        String user = "technoGangster";
        String password = "configurationn";

        try {
            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();
          String query=null;

            query ="SELECT Origin from T1DAlleles";

            rs = st.executeQuery(query);
int rowCount=0;
            if (rs.next()) {

                origins[rowCount]=rs.getString("Origin");
                 rowCount++;
            }
} catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Version.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                    }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(Version.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
        int i=0;
        for(i=0;i<origins.length;i++){
            JOptionPane.showMessageDialog(null,origins[i]);
        }
    return origins;

}

最佳答案

代替if,您应该使用while循环,如下所示:

while (rs.next()) {

10-07 13:18