为什么这个SQL没有包含在try Catch中

时间:2015-01-06 12:19:54

标签: java sql try-catch

为什么SQL不在try catch中,问题是我尝试使用两个单独的try catch但是然后SQL语句无法引用FileInputStream变量,这是我提出的替代方案但它似乎没有工作

try {
    final FileInputStream FIS = new FileInputStream(ReturnFile);        

    JButton Submit = new JButton ("Insert");
    Submit.setLocation (430, 335);
    Submit.setSize (150, 25);
    newCarFrame.add(Submit);
    Submit.addActionListener(new ActionListener () {

        public void actionPerformed(ActionEvent e) {
            Connection Con = new DataBaseHandler().GetConnection();
            Statement stmt = Con.createStatement();

            String SQL = "INSERT INTO Cars (`Reg_No`, `Car_Image`, `Make`, `Model`, `Mileage`, `Date_Of_Purchase`, `Price`, `Storage_Location`, `Body_Type`, `Gearbox`, `Age`, `No_Of_Doors`, `Colour`, `Fuel_Type`, `Engine_Size`, `Description`) VALUES ('" + RegNoTextBox.getText() + "', '" + FIS + "', '" + MakeComboBox.getSelectedItem() + "', '" + ModelTextBox.getText() + "', '" + MillageTextBox.getText() + "', '" + DOPField.getText() + "', '" + PriceField.getText() + "', '" + StorageCombo.getSelectedItem() + "', '" + BodyTypesCombo.getSelectedItem() + "', '" + GearBoxValue + "', '" + No_Of_Doors_Combo.getSelectedItem() + "', '" + AgeField.getText() + "', '" + ColourField.getText() + "', '" + PetrolValue + "', '" + EngineSizeField.getText() + "', '" + DescriptionField.getText() + "')";
            System.out.println(SQL);
            //Execute the SQL query
            //TODO Salt the password
            stmt.executeUpdate(SQL);

            Con.close();
            String ShowAllSalesSQL = "SELECT * FROM `Cars`";
            SalesWindow MSW = new SalesWindow (ShowAllSalesSQL);
        }

    });
} catch (FileNotFoundException e2) {
    System.out.println("No File");
    e2.printStackTrace();
} catch (SQLException e1) {

}

非常感谢所有帮助。

2 个答案:

答案 0 :(得分:0)

那是因为当您执行此操作Submit.addActionListener(new ActionListener () { ... }时,您实际上是以内联方式实现ActionListener接口,作为匿名类。在try块中,您只将动作侦听器实例设置为Submit按钮,实际上并不包围单击按钮时将执行的代码。

答案 1 :(得分:0)

对不起我是傻瓜我修好了,如下图所示

JButton Submit = new JButton ("Insert");
    Submit.setLocation (430, 335);
    Submit.setSize (150, 25);
    newCarFrame.add(Submit);
    Submit.addActionListener(new ActionListener () {

        public void actionPerformed(ActionEvent e) {
            try {

                final FileInputStream FIS = new FileInputStream(ReturnFile);

                Connection Con = new DataBaseHandler().GetConnection();

                Statement stmt = Con.createStatement();

                String SQL = "INSERT INTO Cars (`Reg_No`, `Car_Image`, `Make`, `Model`, `Mileage`, `Date_Of_Purchase`, `Price`, `Storage_Location`, `Body_Type`, `Gearbox`, `Age`, `No_Of_Doors`, `Colour`, `Fuel_Type`, `Engine_Size`, `Description`) VALUES ('" + RegNoTextBox.getText() + "', '" + FIS + "', '" + MakeComboBox.getSelectedItem() + "', '" + ModelTextBox.getText() + "', '" + MillageTextBox.getText() + "', '" + DOPField.getText() + "', '" + PriceField.getText() + "', '" + StorageCombo.getSelectedItem() + "', '" + BodyTypesCombo.getSelectedItem() + "', '" + GearBoxValue + "', '" + No_Of_Doors_Combo.getSelectedItem() + "', '" + AgeField.getText() + "', '" + ColourField.getText() + "', '" + PetrolValue + "', '" + EngineSizeField.getText() + "', '" + DescriptionField.getText() + "')";
                System.out.println(SQL);
                //Execute the SQL query
                //TODO Salt the password
                stmt.executeUpdate(SQL);

                Con.close();

                String ShowAllSalesSQL = "SELECT * FROM `Cars`";

                SalesWindow MSW = new SalesWindow (ShowAllSalesSQL);

            } catch (SQLException e1) {
                e1.printStackTrace();
            } catch (FileNotFoundException e3) {
                e3.printStackTrace();
            }
        }
    });