我正在建立一个库存管理系统,并创建了一个按钮,用于打开一个新的FXML,用于将新项目输入数据库。我已经创建了添加图像的功能,并使用URL更新数据库的图像列,当我单击“保存”时,它会将图像从源位置保存到目标位置(这是程序中的包)。如果我保存并关闭程序,那么我可以在eclipse中刷新项目,重新登录程序,新添加的项目将显示在从数据库填充的tableView中,没有任何问题。
我遇到的问题是,当我点击保存按钮时,我希望它“刷新”程序文件和软件包,所以当我查询零件数据库时,它会显示新添加的零件,而不是因为没有给我一个错误看到我创建的图像包中的图像(它在程序关闭和刷新时显示在那里)。是否可以使用代码刷新这些图像包?图像应该存储在外部文件中吗?如果没有,是否可以将图像保存到程序包而不必使用绝对路径?
我到目前为止的代码是:
// Event Listener on Button[#saveItemBtn].onAction
@FXML
public void saveNewItemClicked(ActionEvent event) {
try{
this.dbState = (String) databaseChoiceBox.getValue();
DBConnection dBC = new DBConnection();
con = dBC.getDBConnection();
String maxSQL = "SELECT MAX(id) FROM " + dbState + "_parts_list";
ResultSet rs = con.createStatement().executeQuery(maxSQL);
int idNumber = rs.getInt(1);
idNumber++;
quantity = Integer.parseInt(quantityTxt.getText());
min = Integer.parseInt(minTxt.getText());
max = Integer.parseInt(maxTxt.getText());
double price = Double.parseDouble(priceTxt.getText());
SelectedItem selectedItem = new SelectedItem();
selectedItem.setId(idNumber);
selectedItem.setQuantity(quantity);
selectedItem.setMin(min);
selectedItem.setMax(max);
selectedItem.setEquipment_id(equipmentIDTxt.getText());
selectedItem.setEquipment_group(equipmentGroupTxt.getText());
selectedItem.setPrice(price);
selectedItem.setManufacturer_name(manufacturerNameTxt.getText());
selectedItem.setModel_number(modelNumberTxt.getText());
selectedItem.setVendor_name(vendorNameTxt.getText());
selectedItem.setVendor_part_number(vendorPartNumberTxt.getText());
selectedItem.setTolmar_part_number(tolmarPartNumberTxt.getText());
selectedItem.setDescription(descriptionTxt.getText());
selectedItem.setAdditional_notes(additionalNotesTxt.getText());
selectedItem.setPart_location(locationTxt.getText());
if (img != null) {
selectedItem.setImage("/" + dbState + "Images/" + imagePath);
} else {
selectedItem.setImage("/img/NoImageFound.png");
}
selectedItem.setDepartment(databaseChoiceBox.getValue().toLowerCase());
String sql = "INSERT INTO " + dbState + "_parts_list VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement myStmt = con.prepareStatement(sql);
myStmt.setInt(1, idNumber);
myStmt.setString(2, selectedItem.getManufacturer_name());
myStmt.setString(3, selectedItem.getModel_number());
myStmt.setString(4, selectedItem.getVendor_name());
myStmt.setString(5, selectedItem.getVendor_part_number());
myStmt.setString(6, selectedItem.getTolmar_part_number());
myStmt.setString(7, selectedItem.getPart_location());
myStmt.setDouble(8, selectedItem.getPrice());
myStmt.setInt(9, selectedItem.getQuantity());
myStmt.setInt(10, selectedItem.getMin());
myStmt.setInt(11, selectedItem.getMax());
myStmt.setString(12, selectedItem.getImage());
myStmt.setString(13, selectedItem.getEquipment_group());
myStmt.setString(14, selectedItem.getEquipment_id());
myStmt.setString(15, selectedItem.getAdditional_notes());
myStmt.setString(16, selectedItem.getDescription());
myStmt.setString(17, selectedItem.getDepartment());
myStmt.executeUpdate();
saveStatusLbl.setText("New Item Saved");
Path source = Paths.get(img);
Path target = Paths.get("E:/Programming/workspace/Inventory Management System 4/src/" + dbState + "Images/", file.getName());
try {
//replace existing file using java nio package
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
con.close();
PauseTransition delay = new PauseTransition(Duration.seconds(2));
delay.setOnFinished( e -> window.close() );
delay.play();
}catch(Exception exc){
System.out.println(exc);
exc.printStackTrace();
}
}
答案 0 :(得分:0)
您绝对不应将数据文件存储在类文件中。在您捆绑应用程序的实际场景中,无论如何都无法正常工作。
答案 1 :(得分:0)
在运行应用程序时,应避免修改应用程序的资源。
当然,您可以尝试通过使用ClassLoader
来解决问题。使用URLClassLoader
,但恕我直言,不将文件存储为资源要简单得多,而是将它们存储在不在类路径内的目录中,而是访问这些文件。您可以使用以下命令从Path p
获取网址字符串:
p.toUri().toURL().toExternalForm()