我是java的新手,我试图打印表中的所有数据
Statement stmt=connection.createStatement();
String sql ="INSERT INTO contacts(name,email)VALUES('ABC','ABC@yahoo.com')";
stmt.execute(sql);
ResultSet rs=stmt.executeQuery("SELECT*FROM contacts");
System.out.println(rs.getString("name"));
得到了
Exception in thread "main" org.h2.jdbc.JdbcSQLException: No data is available [2000-173]
答案 0 :(得分:1)
您需要在致电rs.next()
之前致电rs.getString("...")
。
...
ResultSet rs=stmt.executeQuery("SELECT * FROM contacts");
while(rs.next()) {
System.out.println(rs.getString("name"));
}
如果您只期望一行,那么您也可以这样做:
...
ResultSet rs=stmt.executeQuery("SELECT * FROM contacts");
if(rs.next()) {
System.out.println(rs.getString("name"));
}