简体   繁体   English

Java应用程序-如何显示NetBeans中mysql数据库中的所有记录?

[英]Java application - How to display all the records from mysql database in netbeans?

I`m having trouble with showing all the records from my database... I got database 'library' and table 'books'. 我在显示数据库中的所有记录时遇到了麻烦...我得到了数据库“库”和表“ books”。 Structure of 'members' : id, title, description, author, publisher, yearPublished. “成员”的结构:ID,标题,描述,作者,发布者,年份。

Here is my java code method: 这是我的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"));

        }

when i run this method preuzmi(); 当我运行此方法preuzmi(); in main class it shows me only titles from db... 在主类中,它仅显示db中的标题...
How can i show other records??? 我如何显示其他记录??? Please help me. 请帮我。 THANKS IN ADVANCE!!! 提前致谢!!!

You are missing braces around your while-statement. 您在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"));
}

When you don't put braces after a while, if, for, etc. only the directly following statement will be executed. 如果一段时间后不放置括号,则仅执行以下语句。 The rest will be executed after the entire loop is over. 其余的将在整个循环结束后执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM