在MySQL数据库中选择

时间:2013-04-14 07:09:42

标签: java mysql database jdbc

只有在从rs.getString 1中选择结果后才能在下一行显示rs.getString2?

Statement statement = con.createStatement();
     ResultSet rs = statement.executeQuery("select * from state");
     while ( rs.next() ){
       System.out.println(rs.getString(1) + " " +rs.getString(2)); 

1 个答案:

答案 0 :(得分:0)

Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery("select * from state");
while ( rs.next() ){
    StringBuilder br = new StringBuilder();
    br.append(rs.getString(1)); //print the column 1 in the current row
    br.append(" "); //add space
    rs.next(); //move to the next row
    br.append(rs.getString(2)); //print the column 2 in the next row
    rs.previous(); //go back to the current row of this current iteration
    System.out.println(br.toString()); //print
}