Reputation: 91
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!!!
Upvotes: 1
Views: 24174
Reputation: 13331
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.
Upvotes: 3