java连接数据库查询结果不显示第一条
来源:百度知道 编辑:UC知道 时间:2024/06/08 23:30:03
连接的为access数据库源代码如下:
if (e.getSource() == subMenu3[0]) {
classidT.setText("");
nameT.setText("");
sexT.setText("");
ageT.setText("");
callT.setText("");
departmentT.setText("");
if ((classidT.getText().trim()).equals("")) {
String idclass = JOptionPane.showInputDialog("请输入年级");
String strSQL = "select * from member where classid='" +
idclass +
"'";
try {
rs = st.executeQuery(strSQL);
displayres(rs);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "查询失败");
}
if (e.getSource() == subMenu3[0]) {
classidT.setText("");
nameT.setText("");
sexT.setText("");
ageT.setText("");
callT.setText("");
departmentT.setText("");
if ((classidT.getText().trim()).equals("")) {
String idclass = JOptionPane.showInputDialog("请输入年级");
String strSQL = "select * from member where classid='" +
idclass +
"'";
try {
rs = st.executeQuery(strSQL);
displayres(rs);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "查询失败");
}
不能用boolean moreRecords = rs1.next();来判断,这样就把第一条记录跳过去了。你改成
boolean b=false;
while(rs1.next())
{
b=true;//说明有记录
......读取数据的函数
}
if(b)
{
....有记录
}else{
....无记录
}
第一次next已经取出来了。。。。结果被第二次覆盖了。。。
结果集的显示应该用next的迭代器模式。。。
那就仔细看看第一次循环噻。。
DEBUG!
这个其实很简单的`弄清楚next()的功能与处理机制就行了``
rs.next()后,结果集中的游标首先判断结果集中是否还有下一条记录,如果有游标定位到下一条记录位置上``你上面代码在取数据前next()两次,所以是从第二条记录取数据`
rs1.next();
这时游标已经向下一行了...
while(rs.next()){
这里直接去第一条到最后一条数据
}