我遇到了一個問題,據我所知,這整個代碼的工作,直到pst.executeQuery
我只是不明白爲什麼這是問題。我有一個更新/插入方法,並沒有關於他們的問題。我正在使用標籤來顯示圖像,所以它會是photoLabel.setIcon();
還是我完全偏離軌道?我有一個用於文件選擇器的按鈕,將圖像加載到標籤中,保存按鈕(此功能)應只寫入名爲圖像的12列/字段中的數據庫。Java - JDBC/Swing - executeUpdate不能與BLOB配合使用
請注意 - System.out.Println
用於測試目的,只是
System.out.println("Working 5");
不會顯示,因此爲什麼我知道這事做pst.executeQuery()。我試過搜索網頁,使用了不同的.execute方法,我試圖提交連接等等。唉,沒有運氣。
@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");
}
}});
這是選擇通過使用一個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");
}
}
});
編輯圖像的方法 -
如果不工作,我的意思是,我沒有得到任何錯誤,程序不破。只是圖像不會上傳到數據庫,代碼System.out.println("Working 5");
不會打印到控制檯。所以它似乎在此時被卡住/凍結。
定義*不工作* –
@ScaryWombat我沒有得到任何錯誤,程序不破。只是不會將圖像上傳到數據庫和代碼System.out.println(「Working 5」);不打印到控制檯。 – TheNotoriousCoder
在你的代碼中,你有一個'嘗試' - 你忽略了這個捕獲? –