我无法通过java在数据库中保存图像。我遇到了这个问题。我使用netbeans和mysql来做这个应用程序

时间:2013-12-06 16:01:18

标签: java mysql netbeans jdbc

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Practice.Photo.saveActionPerformed(Photo.java:161)
at Practice.Photo.access$200(Photo.java:20)
at Practice.Photo$3.actionPerformed(Photo.java:68)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

建立成功(总时间:12秒)

这是执行此任务的代码

连接数据库

 private void connectActionPerformed(java.awt.event.ActionEvent evt) {                                        
    try {
        // TODO add your handling code here:
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/forjava","root", "Jilani");
    JOptionPane.showMessageDialog(null,"Connected successfully...!");
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Photo.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        Logger.getLogger(Photo.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        Logger.getLogger(Photo.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(Photo.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null,"Connection failed...!");
    }


    }              

从用户那里获取图像文件

private void chooseActionPerformed(java.awt.event.ActionEvent evt) {                                       
            // TODO add your handling code here:
    filechooser = new JFileChooser();
    filechooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    String[] extensions = ImageIO.getReaderFileSuffixes();
    filechooser.setFileFilter(new FileNameExtensionFilter("Image files",extensions));
    int status = filechooser.showOpenDialog(this);
            filechooser.setMultiSelectionEnabled(false);
            if(status == JFileChooser.APPROVE_OPTION) {
                File file = filechooser.getSelectedFile();
        name=file.getPath();
        path.setText(name);
        try {
            File img=new File(name);
        FileInputStream fis=new FileInputStream(img);
        ByteArrayOutputStream bao=new ByteArrayOutputStream();
        byte[] buf=new byte[1024];
        for(int rdnum;(rdnum=fis.read(buf))!=-1;) {
            bao.write(buf, 0, rdnum);
        }
        image=bao.toByteArray();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Photo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Photo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    }  

最后保存

  private void saveActionPerformed(java.awt.event.ActionEvent evt) {                                     
            // TODO add your handling code here:
    String sql="INSERT INTO photo(image) VALUES(?)";
    try {
        stmt.setBytes(1, image);
        stmt=con.prepareStatement(sql);
        stmt.executeUpdate();
        JOptionPane.showMessageDialog(null,"Saved Successfully into Database...!");
    } catch (SQLException ex) {
        Logger.getLogger(Photo.class.getName()).log(Level.SEVERE, null, ex);
    }                   
    } 

首先用户点击连接数据库,然后弹出一个连接成功,然后用户通过选择按钮选择图像然后点击保存到数据库它将被存储到数据库但是没有发生它显示我错误。请帮助我解决这个问题,我在谷歌很多搜索但我没有解决它。

1 个答案:

答案 0 :(得分:0)

private void saveActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
    String sql="INSERT INTO photo(image) VALUES(?)";
    try {
        stmt.setBytes(1, image);
        stmt=con.prepareStatement(sql);
        stmt.executeUpdate();
        JOptionPane.showMessageDialog(null,"Saved Successfully into Database...!");
    } catch (SQLException ex) {
        Logger.getLogger(Photo.class.getName()).log(Level.SEVERE, null, ex);
    }                   
} 

您在准备语句之前尝试拨打stmt.setBytes(1, image),因此stmtnull。这需要

private void saveActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
    String sql="INSERT INTO photo(image) VALUES(?)";
    try {
        stmt=con.prepareStatement(sql);
        stmt.setBytes(1, image);
        stmt.executeUpdate();
        JOptionPane.showMessageDialog(null,"Saved Successfully into Database...!");
    } catch (SQLException ex) {
        Logger.getLogger(Photo.class.getName()).log(Level.SEVERE, null, ex);
    }                   
}