I have encountered a problem, as far as I know this whole code works up until pst.executeQuery I just can't see as to why this is the issue. I have an update/insert method and no issues regarding them. I am using a label to display the image, so would it be photoLabel.setIcon();or am I just completely off-track. I have one-button for the file chooser which loads the image into the label, the save button (this function) should just write to the database in the 12 column/field called Images.
Please Note - The System.out.Printlnfor testing purposes, just
System.out.println("Working 5");
Will not show, hence why I know it's something to do with pst.executeQuery(). I've tried searching the web, used different .execute methods, I've tried to commit the connection and so on. Alas no luck.
@Override
public void actionPerformed(ActionEvent e) {
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
// String s = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:employeeDatabase.sqlite");
//connection.setAutoCommit(false);
System.out.println("Working 1");
InputStream is = new FileInputStream(new File(s));
System.out.println("Working 2");
String sql = "insert into employees(Images) values(?)";
PreparedStatement pst = connection.prepareStatement(sql);
System.out.println("Working 3");
pst.setBlob(12, is);
System.out.println("Working 4");
pst.executeQuery();
connection.commit();
System.out.println("Working 5");
JOptionPane.showMessageDialog(null, "Data Inserted");
}
}
catch ( Exception e1 ) {
JOptionPane.showMessageDialog(null, "Error");
}
}});
This is the method to select the image via the use of a JFileChooser
uploadImage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.JPG", "jpg","gif","png");
fileChooser.addChoosableFileFilter(filter);
fileChooser.addChoosableFileFilter(filter);
fileChooser.setMultiSelectionEnabled(false);
int result = fileChooser.showSaveDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
File selectedFile = fileChooser.getSelectedFile();
String path = selectedFile.getAbsolutePath();
photoLabel.setIcon(ResizeImage(path));
s = path;
}
else if(result == JFileChooser.CANCEL_OPTION){
System.out.println("No Data");
}
}
});
Edit -
By not working I mean, I get no errors, program doesn't break. Just the image will not upload to the database and the code System.out.println("Working 5"); doesn't print to console. So it appears to be stuck/freeze at that point on.
try- are you ignoring the catch?pst.executeQueryand then got to thecatch.