【问题标题】:Why is this SQL not contained in the try Catch为什么try Catch中不包含这条SQL
【发布时间】:2015-01-06 12:19:53
【问题描述】:

为什么 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) {

}

非常感谢所有帮助。

【问题讨论】:

  • 你遇到了什么错误??
  • 我很困惑。你想做什么,你想达到什么目标?
  • sql 在 try-catch 块中。
  • @Prashant 他的SQLException从未被记录或打印。 OP 将永远不会看到来自 SQLException 的任何异常,如果有的话。
  • 我怀疑您的部分问题是您在 try 范围内声明局部变量,然后尝试在该范围之外引用它们。您要在try(或catch)范围之外引用的任何变量都必须在该范围之外声明。

标签: java sql try-catch


【解决方案1】:

那是因为当你这样做 Submit.addActionListener(new ActionListener () { ... } 时,你实际上是在实现一个 ActionListener 内联接口,作为匿名类。在try 块中,您只是将动作侦听器实例设置为Submit 按钮,实际上并没有包围单击按钮时将执行的代码。

【讨论】:

  • 你对如何让 SQL 字符串连接 FileInputStream 有什么建议
  • 您可能应该将整个try-catch 块连同FIS 一起移动到actionPerformed() 方法中。
  • 我试过了,但它说我需要另一个输入流的 try catch,然后我遇到了同样的问题
  • FIS声明和SQL代码放在同一个try块中,并添加两个catch块,一个用于FileNotFoundException,另一个用于SQLException
【解决方案2】:

对不起,我是愚蠢的,我修复了它,如下所示

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();
            }
        }
    });

【讨论】:

  • 请注意:该代码尚未完成,它缺少一些资源清理代码。它应该使用 try-with-resources 或使用 finally 子句关闭 FIS、Con 和 stmt。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多