Results 1 to 14 of 14
  1. #1
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default Problem in image insertion.

    File file = new File("C:/Users/akm/Desktop/server/flower1.jpg");
    FileInputStream fs = new FileInputStream(file);
    PreparedStatement ps = conn.prepareStatement("INSERT INTO picture(picname,picsize,pictype,image) VALUES(?,?,?,?)");
    ps.setString(1,file.getName());
    pw.println(file.getName());
    ps.setInt(2,89);
    ps.setString(3,"jpeg");
    ps.setBinaryStream(4,fs,fs.available());
    int i = ps.executeUpdate();
    pw.println("value of i is:"+i);

    the updation is not done in the program.the value of i is not getting printed. what is the error in the program.

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

    Default

    Is this line being executed?
    Java Code:
    pw.println(file.getName());
    If so then you are probably getting an exception which you are probably eating.
    Can you show the try/catch/finally part of your code?

    I suspect it's down to the use of fs.available(), which does not give the full length of the file stream, but simply the number of bytes currently available. Don't bother with that parameter.

  3. #3
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    the statement
    pw.println(file.getName()); is getting executed.

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

    Default

    Then an exception is getting thrown.

  5. #5
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    How to know the exception when it is not getting displayed?

  6. #6
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    this statement is alone not getting executed ...ps.setBinaryStream(4,(InputStream)fs,(int)(file.le ngth()));

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

    Default

    Are you eating the exception?
    If not then where are you printing the stack trace to?

  8. #8
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    catch (Exception e)
    {
    System.out.println(e);
    }

    this the catch statement.

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

    Default

    That should be e.printStackTrac(), otherwise you lose lots of useful information in the exception.
    So...where is that going?

  10. #10
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    i've written the e.printStackTrac(); statement..but nothing is printed.

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

    Default

    If you have:
    Java Code:
    try {
        ... some code here ...
        System.out.println("Got here");
        ... some more code here ...
        System.out.println("Now Here");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    and you get "Got here" printed out, but not "Now Here" and there is no return statement or other break statement in the inbetween code, then it will have thrown an exception.
    If you're not seeing it, it is merely because you don't know where your System.out is pointing to. Which is something you'll have to figure out. Until you do I can't help you, as I don't know what exception you are getting.

  12. #12
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    ps.setBinaryStream(4,(InputStream)fs); is not getting executed.

  13. #13
    KiranA is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    try
    {
    Class.forName("com.mysql.jdbc.Driver").newInstance ();
    conn=DriverManager.getConnection("jdbc:mysql://localhost/employee","root"," ");
    stmt=conn.createStatement();
    File file = new File("C:\\Users\\Desktop\\server\\flower1.jpg");
    FileInputStream fs = new FileInputStream(file);
    PreparedStatement ps = conn.prepareStatement("insert into picprop(picname,picsize,pictype,image) values(?,?,?,?)");
    //ps.setInt(1,1);
    ps.setString(1,"yellow");
    ps.setInt(2,89);
    ps.setString(3,"jpeg");
    ps.setBinaryStream(4,(InputStream)fs);
    int i=ps.executeUpdate();;
    pw.println("value of i is:"+i);
    if(i!=0)
    {
    pw.println("image inserted successfully123");
    }
    else
    {
    pw.println("problem in image insertion");
    }
    ps.close();
    fs.close();
    rs.close();
    stmt.close();
    conn.close();
    }

    catch (Exception e)
    {
    e.printStackTrace();

    }

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

    Default

    You are getting an exception...it is going somewhere.
    Presuming this is a webapp (which you haven't said) and pw is going to the response straem, then the exception stack trace will likely be in the server logs or the console output for the server.

    Constantly telling me that the same line is not executing is not helping...find that stack trace. Find that log file.

Similar Threads

  1. image insertion error
    By KiranA in forum New To Java
    Replies: 16
    Last Post: 07-20-2011, 05:33 PM
  2. image insertion
    By KiranA in forum Java Servlet
    Replies: 1
    Last Post: 07-20-2011, 12:58 PM
  3. problem d'insertion
    By haykel9 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 05-04-2010, 11:11 AM
  4. Insertion Problem
    By balaji csc in forum Web Frameworks
    Replies: 0
    Last Post: 12-08-2009, 03:26 PM
  5. image insertion into database
    By abhiN in forum JDBC
    Replies: 4
    Last Post: 01-05-2009, 05:09 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
  •