0

我有一个 sqlite 数据库的接口。一切正常,除了两个 JLabel 不会显示它们应该显示的内容。它在 netbeans 中运行,没有例外。但是当我从命令行运行它时,它不会将文本设置为 JLabels,而只有这两个 JLabels(vanOil 和 VanTires)。有人知道为什么吗?

public class MaintenanceDB {
    String van = (String) TabbedPane.chooseVan.getSelectedItem();

    public void checkVan() throws Exception {
        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager
                .getConnection("jdbc:sqlite:office.sqlite");
        Statement stat = conn.createStatement();
        ResultSet rs = stat
                .executeQuery("Select * from Maintenance WHERE Van IS " + van
                        + ";");

        int totalMiles = Integer.parseInt(TabbedPane.vanMiles.getText());
        int lastOil = rs.getInt("LastOilChange");
        int lastTire = rs.getInt("LastTireRotation");
        int tireIndex = 5;
        int oilIndex = 5;
        int nextTire = lastTire + tireIndex;
        int nextOil = lastOil + oilIndex;
        boolean checkOil = CheckMaintenance.checkOil(totalMiles, nextOil);
        boolean checkTire = CheckMaintenance.checkTire(totalMiles, nextTire);
        int oilDiff = Math.abs(nextOil - totalMiles);
        int tireDiff = Math.abs(nextTire - totalMiles);
        String displayOil = van + " not due for oil change for " + oilDiff
                + " miles.";
        String displayTire = van + " not due for tire totation for " + tireDiff
                + " miles.";
        if (checkOil) {
            displayOil = "**Van " + van + " " + oilDiff
                    + " miles overdue for oil change**";
        }
        if (checkTire) {
            displayTire = "**Van " + van + " " + tireDiff
                    + " miles overdue for tire rotation*";
        }
        rs.close();

        conn.close();
        TabbedPane.vanOil.setText(displayOil);
        TabbedPane.vanTires.setText(displayTire);

    }
}

*编辑** * ** * ****

    if(command.equals(TabbedPane.setMiles.getText())){

             try {
            MaintenanceDB mdb=new MaintenanceDB();
          mdb.checkVan();
        } catch (Exception ex) {
           Logger.getLogger(CallsEvent.class.getName()).log(Level.SEVERE, null, ex);
      }

    }
4

1 回答 1

1

添加以下代码行

System.out.println( "displayOil :" + displayOil );

就在您创建每个变量之后。你会知道你的代码是否到达那里。

如果消息未输出到控制台,则异常正在中断您的代码。

否则,您的方法有

throws Exception

宣言。这不是好的代码实践,因为我们不知道该方法可以抛出哪些异常(因此,它可能在哪里出现问题)。您应该在异常声明中更加具体:例如。

throws JDBCException, BusinessException

这样,您就知道会发生什么。另外,永远不要做

catch(Exception ex) {}

由于这条线,您可以浪费数小时和数天的调试时间(取决于项目的大小)。如果您真的不知道该怎么做,请写入堆栈跟踪以重新抛出 RTex,将其传递给原始原因异常

throw new RuntimeException("don't know what to do here", ex);

最好的问候,Zied Hamdi - http://1vu.fr

于 2013-07-20T00:21:01.290 回答