为什么我在插入照片时会在dataBase中获得空值?

时间:2016-01-23 10:27:53

标签: java mysql

我尝试将照片插入数据库,但我的数据库只显示空值。 这是我的代码的一部分,单击按钮从我的文件中选择照片,将显示照片。在此先感谢您的帮助

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;


}

1 个答案:

答案 0 :(得分:0)

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

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

在第二部分中,您使用相同的对象插入db。