2016-12-21 23 views
0

我遇到了一個問題,據我所知,這整個代碼的工作,直到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");不會打印到控制檯。所以它似乎在此時被卡住/凍結。

+0

定義*不工作* –

+0

@ScaryWombat我沒有得到任何錯誤,程序不破。只是不會將圖像上傳到數據庫和代碼System.out.println(「Working 5」);不打印到控制檯。 – TheNotoriousCoder

+0

在你的代碼中,你有一個'嘗試' - 你忽略了這個捕獲? –

回答

2

首先,雙檢查表employees和列Images表結構與正確類型

然後,設置用於輸入正確的佔位符在準備語句,僅存在一個

pst.setBlob(1, ... 

然後,使用這種方法,而不是

private byte[] readFile(String file) { 
    ByteArrayOutputStream bos = null; 
    try { 
     File f = new File(file); 
     FileInputStream fis = new FileInputStream(f); 
     byte[] buffer = new byte[1024]; 
     bos = new ByteArrayOutputStream(); 
     for (int len; (len = fis.read(buffer)) != -1;) { 
      bos.write(buffer, 0, len); 
     } 
    } catch (FileNotFoundException e) { 
     System.err.println(e.getMessage()); 
    } catch (IOException e2) { 
     System.err.println(e2.getMessage()); 
    } 
    return bos != null ? bos.toByteArray() : null; 
} 

隨着

pst.setBytes(1, readFile(s)); 

最後,調用

pst.executeUpdate(); 

來源:http://www.sqlitetutorial.net/sqlite-java/jdbc-read-write-blob/

+0

好吧,你失去了我與第一,在準備好的語句中設置正確的佔位符輸入,只有一個。但是謝謝你的回覆。 :) – TheNotoriousCoder

+0

閱讀此http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html它取決於#of?在聲明中。第一個是1秒是2等等。 –

+0

我已經添加了代碼,但得到一個私人字節錯誤[] readFile(Stringfile)=令牌「readFile」,AnnotationName預期的語法錯誤 – TheNotoriousCoder

相關問題