【问题标题】:Displaying from database issue从数据库问题显示
【发布时间】:2014-02-17 11:50:18
【问题描述】:

我正在使用 Java 在 Eclipse Indigo 中做作业。我必须为一所学校创建一个程序,该程序是一个拼写测试程序。我已经能够允许教职员工和学生登录并更改他们的密码和编辑他们的帐户。我还能够创建测试并访问它们进行编辑。但我不能做的一件事是访问列表中的可用测试以查看设置了多少测试。我不知道要在我的代码中更改什么来解决这个问题。 为了显示测试,我创建了一个名为 displayTest 的方法。我使用 HashFile 为测试创建了存储。

在这里,我已将测试的存储空间保存在 HashFile 中。

String test_FILENAME = "datafiles/test.dat";
HashFile testFile = new HashFile(test_FILENAME, 100, new Test());

我继续使用以下方法添加测试(我的编码文档顶部有变量 test 的存储空间):

public void addTest() {
   Test test = new Test();// create storage for a teacher
   test.input(); // read the teacher details in through the keyboard
   testFile.store(test); // store the teacher in the teacher file

我还可以使用以下方法编辑帐户(PC 等来自我使用的另一个类):

public void editTest() {
  String testNo;
  PC_Dialog editTests = new PC_Dialog("Edit Test", "Test No", "Find");// create a dialog box to ask for the teacher number
  editTests.choice();// display the dialog box
  testNo = editTests.getField(21); // get the teacherNo that has been entered
  test = (Test) testFile.retrieve(testNo); // retrieve the teacher from the file
  if (test != null) {// check if the teacher is found
     test.edit();// yes so display it for editing
     testFile.store(test); // store the teacher back in the teacher file
  } else
     JOptionPane.showMessageDialog(null, "Unable to find the test!");// not found so give an error message
}// end of method

但是在 displayTest() 我有一些问题。 displayTest 旨在允许教师查看所有测试集,以查看哪些要编辑,哪些只需单击按钮即可删除。但问题是 if (test.year == year) { 在第一个 year 下方有一条红线,当我将鼠标悬停在它,它说'年份无法解决或不是一个字段'。谁能帮我尝试让 displayTest 部分从数据文件中收集测试并将它们显示在表格上?我是 Stackoverflow 的新手,一年多前只使用过一次。如果有人对新人的工作感到沮丧,我深表歉意,但这实际上就像我第一次使用 Stackoverflow。非常感谢您的帮助,谢谢。

我已经更改了代码:

public void displayTest() {
  Test testNo = new Test();
  Test yearGroup = new Test();
  Test date = new Test();
  PC_Table table = new PC_Table("Tests Available", 0, "Test Number, Date, Year Group", "OK");
  int row = 1;// a counter for the current row
  // prepare the file for serial access
  testFile.resetSerialAccess();
  // loop through all the records in the event file
  while (testFile.moreSerialRecords()) {
     // read the next record
     Test test = (Test) testFile.getNextSerialValue();{
        // retrieve the reason
        // add a row to the table and put the values in
        table.addRow();
        table.setValueAt(test.testNo, row, 1);
        table.setValueAt(test.date, row, 2);
        table.setValueAt(test.yearGroup, row, 3);
        row++;
  }
  table.choice();
  }

 }// end of method

测试数据库(测试类)中没有任何内容显示在表格上。

【问题讨论】:

    标签: java eclipse eclipse-indigo


    【解决方案1】:

    你有

    Test date = new Test();
    

    “测试”是类的名称。 “日期”是该类实例的名称。 你说错误在

    if (test.year == year)
    

    我没有在代码中看到那行,但你想要的是

    if (date.year == year)
    

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-03
      • 1970-01-01
      • 2016-10-25
      • 2011-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多