我无法在jsp中显示数据库的最后一行。我已经尝试打印rs.getString(1)
,但它不起作用。
ResultSet rs = st.executeQuery("Select MAX(CustomerID) from Customer");
while(rs.next()){out.print(rs.getString(1));}
答案 0 :(得分:1)
我不知道这是否是你想要的,但如果你想要获得整行,有一些方法可以实现这一目标
ResultSet rs = st.executeQuery("select max(customerid) as id from customer");
rs.next();
String id = rs.getString("id");
rs = st.executeQuery("select field_a, field_b from customer where customerid = " + id);
rs.next();
String row = id + "," + rs.getString("field_a") + "," + rs.getString("field_b");
out.println(row);
当然,您需要将field_a
和field_b
列替换为customer
表格中的列,并根据需要添加或删除列。
较短的方法是使用order by
和limit
这样的关键字
ResultSet rs = st.executeQuery("select customerid, field_a, field_b from customer order by customerid desc limit 1");
rs.next();
String row = rs.getString("customerid") + "," + rs.getString("field_a") + "," + rs.getString("field_b");
out.println(row);
确保向primary key
列添加unique
或customerid
约束,以提高两种方法的效果。