Results 1 to 3 of 3
  1. #1
    marvelk is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default inserting images in database through the jfilechooser

    how do i insert an image in a database selected with the jfilechooser i tried this code but it does not work




    JFileChooser jf = new JFileChooser();
    jf.showOpenDialog(jLabel22);
    f = jf.getSelectedFile();
    pat = f.getAbsolutePath();
    na = f.getName();
    if (f.length()<=4000){
    picname = f.toString();
    jLabel22.setIcon(new ImageIcon(picname));
    SwingUtilities.updateComponentTreeUI(jLabel22);
    SwingUtilities.updateComponentTreeUI(jf);
    }else
    JOptionPane.showMessageDialog(null, "your picture size is too large pls resize");

    try {
    PreparedStatement ps4 = con.prepareStatement("insert into memberpic values(?,?)");
    String nam = jTextField2.getText();
    try {
    File fob = new File(pat);
    fi = new FileInputStream(fob);
    } catch (FileNotFoundException ex1) {
    Logger.getLogger(CPM.class.getName()).log(Level.SE VERE, null, ex1);
    }
    ps4.setString(1, nam);
    ps4.setBinaryStream(2, fi);
    JOptionPane.showMessageDialog(null, "pic was uploaded sucessfully");
    } catch (SQLException ex1) {
    JOptionPane.showMessageDialog(null, "uploading failed");
    Logger.getLogger(CPM.class.getName()).log(Level.SE VERE, null, ex1);
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    but it does not work
    This is like me asking you why my lawn mower doesn't work...what about it doesn't work? Does it compile? Are there exceptions? This information is important to diagnose the problem you encounter. In this case I can point out something (which I don't know is the problem you are referring to but will be a problem) your code is missing a crucial statement to complete the update: Using Prepared Statements (The Java™ Tutorials > JDBC(TM) Database Access > JDBC Basics)

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Along with what doWhile says, can I just say that this:
    Java Code:
    File fob = new File(pat);
    is pointless, since you already have the File in variable f.

Similar Threads

  1. Problem inserting into database
    By Unnel in forum JDBC
    Replies: 13
    Last Post: 01-23-2011, 09:23 PM
  2. data is not inserting into database
    By gb.rashu in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 08-21-2010, 03:05 PM
  3. Replies: 2
    Last Post: 07-01-2010, 02:32 AM
  4. Replies: 2
    Last Post: 04-01-2010, 04:35 AM
  5. Inserting file in to database
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-07-2008, 08:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •