简体   繁体   中英

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'. Structure of 'members' : id, title, description, author, publisher, yearPublished.

Here is my java code method:

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(); in main class it shows me only titles from db...
How can i show other records??? Please help me. THANKS IN ADVANCE!!!

You are missing braces around your while-statement.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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