I am trying to upload and retrieve an image from sqlite database.
It has been working until I have changed the jar file from sqlite-jdbc-3.23.1
to sqlite-jdbc-3.6.14.1
. The reason why i have changed it is because of the executable jar.The executable jar file was saying -no table users.
Then I browse and get some helpful content from How to include SQLite database in executable Jar? but now I come up with another problem which I have specified it above. the data type of the image is BLOB.
my code to upload and insert an image is as below:
Inserting:
Connection con = AccountSettingController.getCon();
String sql = "update settings set title =? ,image =?";
try {
ps = con.prepareStatement(sql);
fis = new FileInputStream(file);
ps.setString(1, nm);
ps.setBinaryStream(2, (InputStream) fis, (int) file.length());
int x = ps.executeUpdate();
if (x > 0) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("updating system settings");
alert.setHeaderText(null);
alert.setContentText("System setting successfully updated");
alert.showAndWait();
} else {
syserrorl.setText("something went wrong");
}
} catch (SQLException | FileNotFoundException ex) {
System.out.println(ex);
}
Retrieving:
String sq = "select * from settings";
handler = DatabaseHandler.getInstance();
ResultSet result = handler.executeQuery(sq);
String title = null;
while (result.next()) {
title = result.getString("title");
}
InputStream is = result.getBinaryStream("image");
OutputStream os = new FileOutputStream(new File("photo.jpg"));
byte[] content = new byte[1024];
int size = 0;
while ((size = is.read(content)) != -1) {
os.write(content, 0, size);
}
name.setText(title);
}
System.out.println(title);
image = new Image("file:photo.jpg", imgview.getFitWidth(),
imgview.getFitHeight(), true, true);
imgview.setImage(image);
Thanks for your time and considerations!