【发布时间】: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)范围之外引用的任何变量都必须在该范围之外声明。