2016-01-23 43 views
0

我尝试将照片插入数据库,但我的数据库只显示空值。 这是我的代码的一部分,点击按钮从我的文件中选择照片,照片就会显示出来。在此先感谢帮助插入照片时,为什么我会在dataBase中获得空值?

btnFrontPhoto.addActionListener(new ActionListener(){ 
    String s; 
    @Override 
public void actionPerformed(ActionEvent e){ 
     Shopping post = new Shopping(); 
     if(ShoppingDA.createPost(post)){ 
    JFileChooser fileChooser = new JFileChooser(); 
    fileChooser.setCurrentDirectory(new File(System.getProperty("user.home"))); 
    FileNameExtensionFilter filter = new FileNameExtensionFilter("*.IMAGE", "jpg","gif","png"); 
    fileChooser.addChoosableFileFilter(filter); 
    int result = fileChooser.showSaveDialog(null); 
    if(result == JFileChooser.APPROVE_OPTION){ 
     File selectedFile = fileChooser.getSelectedFile(); 
     String path = selectedFile.getAbsolutePath(); 
     lblFrontPhoto.setIcon(ResizeImage(path)); 
     s = path; 
      } 
    else if(result == JFileChooser.CANCEL_OPTION){ 
     System.out.println("No Data"); 
    } 
     } 

} 
    public ImageIcon ResizeImage(String imgPath){ 
     ImageIcon MyImage = new ImageIcon(imgPath); 
     Image img = MyImage.getImage(); 
     Image newImage = img.getScaledInstance(lblFrontPhoto.getWidth(), lblFrontPhoto.getHeight(),Image.SCALE_SMOOTH); 
     ImageIcon image = new ImageIcon(newImage); 
     return image; 
    } 

}); 

}} 

这是插入照片到我的数据库的代码。我不确定哪一部分是问题。

public static boolean createPost(Shopping post){ 
    boolean success = false; 
    DBController db = new DBController(); 
    String dbQuery; 
    PreparedStatement pstmt; 

    db.getConnection(); 

    dbQuery = "INSERT INTO registration2(photo) VALUES(?)"; 
    pstmt = (PreparedStatement) db.getPreparedStatement(dbQuery); 

    try { 
     pstmt.setBlob(1, post.getPhoto()); 

     if (pstmt.executeUpdate() == 1) 
       JOptionPane.showMessageDialog(null, "Data Inserted"); 
      success = true; 
     pstmt.close(); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
    db.terminate(); 
    return success; 


} 

回答

0

在你的第一部分中,你已经创建了Shopping对象'post',但没有为它赋值。

Shopping post = new Shopping(); 
     if(ShoppingDA.createPost(post)){...} 

而在第二部分中,您正在使用相同的对象来插入数据库。

相关问题