为什么这个 SQL 不包含在 try Catch 中
Why is this SQL not contained in the 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) {
}
非常感谢所有帮助。
那是因为当你这样做时 Submit.addActionListener(new ActionListener () { ... }
你实际上是在实现一个 ActionListener
内联接口,作为匿名 class。在那个 try
块中,您只是将动作侦听器实例设置为 Submit
按钮,您实际上并没有包围单击按钮时将执行的代码。
抱歉我太笨了,我按如下所示修复了它
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();
}
}
});
为什么 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) {
}
非常感谢所有帮助。
那是因为当你这样做时 Submit.addActionListener(new ActionListener () { ... }
你实际上是在实现一个 ActionListener
内联接口,作为匿名 class。在那个 try
块中,您只是将动作侦听器实例设置为 Submit
按钮,您实际上并没有包围单击按钮时将执行的代码。
抱歉我太笨了,我按如下所示修复了它
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();
}
}
});