我在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;
}
答案 0 :(得分:0)
而不是if,你应该使用while循环,如下所示:
while (rs.next()) {
答案 1 :(得分:0)
如果您只需要此列的值,请更改条件&#39;如果&#39;循环&#39;而&#39;
e.g。
while (rs.next()) {
origins[rowCount]=rs.getString("Origin");
rowCount++;
}
}