当我从命令行运行时,JLabel不会改变

时间:2013-07-19 23:51:49

标签: java swing sqlite

我有一个sqlite db的接口。一切正常,除了两个JLabel不会显示它们应该是什么。它在netbeans中运行,没有例外。但是当我从命令行运行它时,它不会将文本设置为JLabel,而只有这两个JLabel(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);
      }

    }

1 个答案:

答案 0 :(得分: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);

最诚挚的问候, 齐德哈姆迪 - http://1vu.fr

相关问题