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

    Default image insertion error

    import java.sql.*;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class InsertImage extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    PrintWriter pw = response.getWriter();


    String connectionURL = "jdbc:mysql://localhost:3306/employee";

    Connection con=null;

    try{
    Class.forName("com.mysql.jdbc.Driver").newInstance ();
    pw.println("image11");
    con = DriverManager.getConnection(connectionURL, "root", "");

    PreparedStatement ps = con.prepareStatement("INSERT INTO picture VALUES(?,?)");
    File file = new File("E:/image.jpg");
    FileInputStream fs = new FileInputStream(file);
    ps.setInt(1,1);
    ps.setBinaryStream(2,fs,fs.available());
    int i = ps.executeUpdate();
    if(i!=0){
    pw.println("image inserted successfully");

    }else{
    pw.println("problem in image insertion");
    }
    } catch (Exception e){
    System.out.println(e);
    }
    }
    }

    ..none of the message is displayed on server... can anybody tell me what is the error in it?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,536
    Blog Entries
    7
    Rep Power
    20

    Default

    If not even the message "image11" is printed, the method aborted before that point of execution or the method wasn't even called. Comment out everything in that method and insert a single print statement that prints something like "*** in the method ***" and see if your method is actually called. Work yourself up from there.

    kind regards,

    Jos

    ps.do you really need a newInstance() of your JDBC driver?
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Are you getting an exception in the server logs?
    Is the image being saved?
    Also, when posting code, please use code tags.

    Oh, and you never close your connection, so you'll be leaking resources. Though that isn't part of your current problem.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    And don't multi-post...this one is in the servlets forum as well.
    Where I attempted an answer.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,536
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    And don't multi-post...this one is in the servlets forum as well.
    Where I attempted an answer.
    @Tolls: I moved your other post to this thread and I'm going to close the other thread; thanks for the notification.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Just need to wait for DB to come in with the list of crossposts...

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,536
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Just need to wait for DB to come in with the list of crossposts...
    Darryl: the Van Helsing of the forums!

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Quote Originally Posted by Tolls View Post
    Are you getting an exception in the server logs?
    Is the image being saved?
    Also, when posting code, please use code tags.

    Oh, and you never close your connection, so you'll be leaking resources. Though that isn't part of your current problem.
    "i'm not getting any exception,i'vesaved the image"

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

    Default

    the message "image11" is also not printed.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,536
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by KiranA View Post
    the message "image11" is also not printed.
    Then (re)read my first reply: your method either isn't called at all or it aborts before it could print that message.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    So that leaves something up with your response then.
    Since everything else is working.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    Then (re)read my first reply: your method either isn't called at all or it aborts before it could print that message.

    kind regards,

    Jos
    Except they say the image is saved to the db, so it must be running.
    Though I do have my doubts about how they are testing this.

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,536
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Except they say the image is saved to the db, so it must be running.
    Though I do have my doubts about how they are testing this.
    Darn, I didn't see that reply; but then at least "image11" must've been printed. I know nothing about Servlets (and I like to keep it that way, thank you) but maybe that writer pw should've been closed at the end ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    I was wondering if it should have a flush() call. Of course closing would have the same effect, but I can never remember if the response stream is still needed higher up the chain.

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,536
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    I was wondering if it should have a flush() call. Of course closing would have the same effect, but I can never remember if the response stream is still needed higher up the chain.
    Yep, I googled for the API documentation for the (Http)ServletResponse interface and read this:

    Java Code:
    Calling flush() on the PrintWriter commits the response.
    The OP should issue a flush().

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    You should write Servlets you know...
    ;)

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,536
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    You should write Servlets you know...
    ;)
    Eeeeeww! Go far from me you foul person!

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. image insertion
    By KiranA in forum Java Servlet
    Replies: 1
    Last Post: 07-20-2011, 12:58 PM
  2. Uncaught error fetching image
    By dyelax in forum New To Java
    Replies: 8
    Last Post: 03-21-2011, 12:06 AM
  3. Error in saving an Image????
    By LankanSniper in forum Java 2D
    Replies: 4
    Last Post: 11-07-2009, 04:48 AM
  4. image insertion into database
    By abhiN in forum JDBC
    Replies: 4
    Last Post: 01-05-2009, 05:09 AM
  5. error while loading the image from the server
    By testtest in forum CLDC and MIDP
    Replies: 1
    Last Post: 09-23-2008, 05:16 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
  •