so I have managed to build my custom cell which holds a button to download a file from my local database. somehow all seems fine but when i run and try to download, I get an sql error that there’s a problem with my sql query. I would also like the user to chose the location to save the downloaded file. Any help is highly appreciated.
here is the sql error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '='20'' at line 1
here is the onaction method for the download button
FileDownloadButton.setOnAction(e -> { Item chosenItem = getTableView().getItems().get(getIndex()); File file = new File(chosenItem.getPaperName()); try { String filequery = "Select File from items Where" + "itemID" + " = " + chosenItem.getId(); PreparedStatement pst = connection.prepareStatement(filequery); ResultSet rs = pst.executeQuery(); FileOutputStream fileout = new FileOutputStream(file); while (rs.next()) { InputStream fileinput = result.getBinaryStream("file"); byte[] buffer = new byte[1024]; while (fileinput.read(buffer) > 0) { fileout.write(buffer);
Advertisement
Answer
Here is what has worked for me
FileDownloadButton.setOnAction(e -> { Item chosenItem = getTableView().getItems().get(getIndex()); byte[] buffer = new byte[1024]; String filename = null; try { String filequery = "Select File from items Where itemID = " + chosenItem.getId(); PreparedStatement pst = connection.prepareStatement(filequery); ResultSet rs = pst.executeQuery(); if (rs.next()) { try { InputStream fileIn = rs.getBinaryStream("File"); buffer = rs.getBytes("File"); filename = chosenItem.getPaperName(); FileOutputStream fileout = new FileOutputStream("D:/Downloads/" + filename); while (fileIn.read(buffer) > 0) { fileout.write(buffer); } } catch (FileNotFoundException ex) { /*...*/ } } /*finally open the file*/ Desktop.getDesktop().open(new File("D:/Downloads/" + filename));