我在显示数据库中的所有记录时遇到问题... 我有数据库'库'和表'书'。 '成员'的结构:id,title,description,author,publisher,yearPublished。
这是我的java代码方法:
public static void preuzmi() throws ClassNotFoundException, InstantiationException, SQLException, IllegalAccessException{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/library", "root","");
Statement st = conn.createStatement();
st.executeQuery("select * from books");
ResultSet rs = st.getResultSet();
while(rs.next())
System.out.println(rs.getString("title"));
System.out.println(rs.getString("description"));
System.out.println(rs.getString("author"));
System.out.println(rs.getString("publisher"));
System.out.println(rs.getString("yearPublished"));
}
当我运行这个方法preuzmi();在主类中它只显示来自db的标题...
我怎么能显示其他记录?请帮我。感谢提前!!!
答案 0 :(得分:3)
你在while语句周围缺少大括号。
while(rs.next()) { // this tiny little '{' is reeeaaaally important here
System.out.println(rs.getString("title"));
System.out.println(rs.getString("description"));
System.out.println(rs.getString("author"));
System.out.println(rs.getString("publisher"));
System.out.println(rs.getString("yearPublished"));
}
如果你在一段时间后没有括号,if,for等,只会执行直接跟随的语句。其余的将在整个循环结束后执行。