0

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!

2
  • 1
    Post the entire error message and stack trace you are getting. You don't need to downgrade your SQLite JDBC driver because a different version was indicated in the Stack Overflow question you provided a link to in your question. That question is seven years old. I'm guessing that the older JDBC driver probably does not support BLOB data type. I suggest you do as they say in that question but use the most up-to-date JDBC driver instead of the one indicated in that old question. Commented Nov 15, 2019 at 16:27
  • Abra,Thanks for your suggestion.I have tried it with another way with the up-to-date JDBC driver and works fine. Commented Nov 17, 2019 at 13:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.