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

    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();
    f = jf.getSelectedFile();
    pat = f.getAbsolutePath();
    na = f.getName();
    if (f.length()<=4000){
    picname = f.toString();
    jLabel22.setIcon(new ImageIcon(picname));
    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
    Rep Power


    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
    Rep Power


    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, 10: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, 04:05 PM
  3. Replies: 2
    Last Post: 07-01-2010, 03:32 AM
  4. Replies: 2
    Last Post: 04-01-2010, 05:35 AM
  5. Inserting file in to database
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-07-2008, 09: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