这是查询部分
@FXML
void dbsave(ActionEvent event) throws SQLException {
String user = parUsertxt.getText();
String first = parFirstNametxt.getText();
String last = parLastNametxt.getText();
String cnic = parCnictxt.getText();
String date = parDatetxt.getValue().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String address = parAddresstxt.getText();
String status = parStatustxt.getText();
String province = parProvincetxt.getText();
String district = parDistricttxt.getText();
String tehsil = parTehsiltxt.getText();
String area = parAreatxt.getText();
String phone = parPhonetxt.getText();
String ephone = parEmPhonetxt.getText();
String gender = parGendertxt.getSelectionModel().getSelectedItem();
String father = parFatherNametxt.getText();
String fathercnic = parFatherCNICtxt.getText();
// BinaryStreamValueFactory personimage;// how can i use this to store an image
String query = "INSERT INTO `landrecord`.`parowner` (`UserName`, `FirstName`, `LastName`, `CNIC`, `DateOfBirth`, `Address`, `Status`, `Province`, `Tehsil`, `District`, `Area`, `PhoneNo`, `EmergencyPhoneNo`, `Gender`, `FatherName`, `FatherCNIC`,'ImagePerson') " +
"VALUES ('" + user + "','" + first + "','" + last + "','" + cnic + "','" + date + "','" + address + "','" + status + "','" + province + "','" + tehsil + "','" + district + "','" + area + "','" + phone + "','" + ephone + "','" + gender + "','" + father + "','" + fathercnic + "')";
if (connectionClass.execAction(query)) {
System.out.println("Data Successful Save");
savebtn.getScene().getWindow().hide();
try {
Parent root = FXMLLoader.load(getClass().getResource("parLandRecord.fxml"));
Stage stage = new Stage();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
} catch (IOException ex) {
System.out.print(ex.getLocalizedMessage());
}
}else{
System.out.println("Insertion Failed.");
}
}
这是在图像视图中显示图像的按钮
@FXML
void personCnicbtn(ActionEvent event) throws MalformedURLException {
fileChooser1 = new FileChooser();
fileChooser1.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Select Image","*.png","*.jpg","*.gif"));
File file1 = fileChooser1.showOpenDialog(null);
if (file1 != null){
imageFile1 = file1.toURI().toURL().toString();
Image image1 = new Image(imageFile1);
personCnicImage.setImage(image1);
}else{
System.out.println("image Error");
}
}
这是我正在处理的代码现在,我需要不使用prepareStatement来保存图像,或者如果有一种使用prepareStatement的方法,那么请告诉我如何?
答案 0 :(得分:1)
如果您将文件存储到数据库中,我建议以blob格式存储。这很好处理。在代码中,我很简单地在代码中提供信息以如何使用准备好的语句进行存储。
Connection connection = null;
PreparedStatement statement = null;
FileInputStream inputStream = null;
File image = new File("your_file_path_which_is_getting_from_FileChooser");
inputStream = new FileInputStream(image);
/**
* establish connection of database.
*/
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_db", "your_username", "your_pw");
statement = connection.prepareStatement("insert into parowner(UserName,FirstName,LastName,CNIC,DateOfBirth,Address,Status,Province,Tehsil,District,Area,PhoneNo,EmergencyPhoneNo,Gender,FatherName,FatherCNIC,ImagePerson) " + "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
statement.setString(1, "UserName");
/**
* set image as byte stream
*/
statement.setBinaryStream(17, (InputStream) inputStream, (int)(image.length()));
statement.executeUpdate();