![](/img/trans.png)
[英]Netbeans Java Desktop Database application with login connecting to users from mysql
[英]Java application - How to display all the records from mysql database in netbeans?
我在显示数据库中的所有记录时遇到了麻烦...我得到了数据库“库”和表“ books”。 “成员”的结构:ID,标题,描述,作者,发布者,年份。
这是我的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中的标题...
我如何显示其他记录??? 请帮我。 提前致谢!!!
您在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"));
}
如果一段时间后不放置括号,则仅执行以下语句。 其余的将在整个循环结束后执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.