JAVA - 我的下一个按钮不能正常工作,但它只返回第一行ID

时间:2015-10-21 11:09:19

标签: java derby

我是java编程的新手,我对我正在做的事情感到困惑,虽然有些是自我解释的,我可以理解。但是凭我的代码,我不知道到底出了什么问题。我可以从数据库中插入数据,但是当我用当前代码单击下一步时,它只返回不填充其他文本框的ID字段,它也返回第1行。

这是我的下一个按钮的代码

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {                                        
    try{
        String url = "jdbc:derby://localhost:1527/mim";
        String username = "is2560";
        String password = "is2560";
        Connection conn = DriverManager.getConnection(url,username,password);
        Statement stmt = conn.createStatement(
                                ResultSet.TYPE_SCROLL_INSENSITIVE,
                                ResultSet.CONCUR_READ_ONLY);
        String query = "SELECT * from students";
        rs = stmt.executeQuery(query);           
        if (rs.next()){
            int ID = rs.getInt("ID");
            String id = Integer.toString(ID);
            String Fname = rs.getString("FNAME");
            String Mname = rs.getString("MNAME");
            String Lname = rs.getString("LNAME");
            String Stdno = rs.getString("STUDENTNO");
            String Course = rs.getString("COURSE");

            txtCourse.setText(Course);
            txtFName.setText(Fname);
            txtMname.setText(Mname);
            txtLname.setText(Lname);
            txtStdnt.setText(Stdno);
            txtID.setText(id);
        }
        else {
            rs.previous();
            JOptionPane.showMessageDialog(this, "End of File");
        }
    }
    catch (SQLException err){
        JOptionPane.showMessageDialog(this, err.getMessage());
    }
}                                       

2 个答案:

答案 0 :(得分:1)

步骤1创建课程Student

public class Student {

  private int mId;
  private String mFirstname;
  private String mLastName;
  private String mMName;
  private String mInfo;
  private String mCourse;
  public int getId() {
    return mId;
  }
  public void setId(int pId) {
    this.mId = pId;
  }
  public String getFirstname() {
    return mFirstname;
  }
  public void setFirstname(String pFirstname) {
    this.mFirstname = pFirstname;
  }
  public String getLastName() {
    return mLastName;
  }
  public void setLastName(String pLastName) {
    this.mLastName = pLastName;
  }
  public String getMName() {
    return mMName;
  }
  public void setMName(String pMName) {
    this.mMName = pMName;
  }
  public String getInfo() {
    return mInfo;
  }
  public void setInfo(String pInfo) {
    this.mInfo = pInfo;
  }
  public String getCourse() {
    return mCourse;
  }
  public void setCourse(String pCourse) {
    this.mCourse = pCourse;
  }
}

第2步在您的班级中定义2个班级成员:

private List<Student> mAllStudent = new ArrayList<>;
private int mClickIndex = 0;

步骤3从德比中加载所有学生:

private void loadAllStudent(java.awt.event.ActionEvent evt) {                                        
    try{
        String url = "jdbc:derby://localhost:1527/mim";
        String username = "is2560";
        String password = "is2560";
        Connection conn = DriverManager.getConnection(url,username,password);
        Statement stmt = conn.createStatement(
                                ResultSet.TYPE_SCROLL_INSENSITIVE,
                                ResultSet.CONCUR_READ_ONLY);
        String query = "SELECT * from students";
        rs = stmt.executeQuery(query);           
        while (rs.next()){
            Student lStudent = new Student();
      lStudent.setId(rs.getInt("ID"));
      lStudent.setFirstname(rs.getString("FNAME"));
      lStudent.setLastName(rs.getString("LNAME"));
      lStudent.setMName(rs.getString("MNAME"));
      lStudent.setInfo(rs.getString("STUDENTNO"));
      lStudent.setCourse(rs.getString("COURSE"));
      mAllStudent.add(lStudent);


        }
        else {
            rs.previous();
            JOptionPane.showMessageDialog(this, "End of File");
        }
    }
    catch (SQLException err){
        JOptionPane.showMessageDialog(this, err.getMessage());
    }
}  

第4步实现nextButton逻辑:

private void showNext(){
   if (mClickIndex == mAllStudent.size()){
      mClickIndex = 0; // start at beginning after end is reached.
   }
   Student lStudent = mAllStudent.get(mClickIndex);
   mClickIndex++;
   if (null != lStudent){
        txtCourse.setText(lStudent.getCourse());
        txtFName.setText(lStudent.getFirstname());
        txtMname.setText(lStudent.getMName());
        txtLname.setText(lStudent.getLastname());
        txtStdnt.setText(lStudent.getInfo());
        txtID.setText(lStudent.getId());
   }
}

在调用showNext之前,你必须在你的类的构造函数中加载所有学生,或者如果你有的话,在init方法中加载。

答案 1 :(得分:0)

兄弟而不是      if (rs.next()){此行必须使用

while(rs.next()){

试试并告诉我