Results 1 to 10 of 10
  1. #1
    seristee is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Problem uploading a PDF to MySQL database

    i am trying to upload a pdf to a mysql database and encountering the following problem..

    Java Code:
    java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
    	at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3711)
    	at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3695)
    	at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3737)
    	at com.mysql.jdbc.PreparedStatement.setInt(PreparedStatement.java:3681)
    	at fpapp.dbtransactions.insertReport(dbtransactions.java:376)
    	at fpapp.main.reportsavebuttonActionPerformed(main.java:1988)
    	at fpapp.main.access$100(main.java:19)
    	at fpapp.main$2.actionPerformed(main.java:307)
    	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:3321)
    	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:2713)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	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:680)
    	at java.awt.EventQueue$4.run(EventQueue.java:678)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    Java Code:
    if (dbconnections.conn != null) {
                try {
                    int result = JOptionPane.showConfirmDialog(null, "Are you sure you want to save this information");
                    if (result == JOptionPane.YES_OPTION) {
                        dbconnections.dconnect();
                        File floorplan = new File(floorplanpath);
                        File reportsubmitted = new File(reportsubmittedpath);
                        File request = new File(requestpath);
                        //Statement stmt = (Statement) dbconnections.conn.createStatement();
                        String sql = "INSERT INTO returnsfront VALUES ('" + "" + "', '" + datayear + "', '" + firstname + "', '" + lastname + "', '" + propertytitle + "', '" + businesstitle + "', '" + address + "', '" + district + "', '" + floorplan + "', '" + reportsubmitted + "', '" + request + "', '" + review + "')";
                        PreparedStatement pstmt = dbconnections.conn.prepareStatement(sql);
                        pstmt.setInt(1, 5);
                        pstmt.setBlob(2, new FileInputStream(floorplan), floorplan.length());
                        pstmt.setBlob(2, new FileInputStream(reportsubmitted), reportsubmitted.length());
                        pstmt.setBlob(2, new FileInputStream(review), review.length());
                        //pstmt.executeUpdate(sql);
                        //JOptionPane.showMessageDialog(null,"Inserted successfully!");
                        System.out.println(sql);
                    } else if (result == JOptionPane.NO_OPTION) {
                        //do nothing
                    } else if (result == JOptionPane.CANCEL_OPTION) {
                        //do nothing
                    }
                } catch(Exception e){
                //JOptionPane.showMessageDialog(null, e);
                //System.err.println(e);
                    e.printStackTrace();
                }
    Any help would be greatly appreciated thanks....

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Problem uploading a PDF to MySQL database

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Problem uploading a PDF to MySQL database

    Moved from NetBeans

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    seristee is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Problem uploading a PDF to MySQL database

    i have a prepared statement

    PreparedStatement pstmt = dbconnections.conn.prepareStatement(sql);

    thanx for your support

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Problem uploading a PDF to MySQL database

    Quote Originally Posted by seristee View Post
    i have a prepared statement

    PreparedStatement pstmt = dbconnections.conn.prepareStatement(sql);

    thanx for your support
    So, problem solved?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    seristee is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: Problem uploading a PDF to MySQL database

    With this code i can insert a blob into my database but it saves as a BIN file which is totally useless.. how can i upload a pdf file and retrieve it as the same file that i uploaded it into the database..
    Java Code:
    if (dbconnections.conn != null) {
                try {
                    int result = JOptionPane.showConfirmDialog(null, "Are you sure you want to save this information");
                    if (result == JOptionPane.YES_OPTION) {
                        dbconnections.dconnect();
                        File floorplan = new File(floorplanpath);
                        File reportsubmitted = new File(reportsubmittedpath);
                        File request = new File(requestpath);
                        //Statement stmt = (Statement) dbconnections.conn.createStatement();
                        String sql = "INSERT INTO reports VALUES ('" + iddata + "', '" + datayear + "', '" + firstname + "', '" + lastname + "', '" + propertytitle + "', '" + businesstitle + "', '" + address + "', '" + district + "', '" + floorplan + "', '" + reportsubmitted + "', '" + request + "', '" + review + "')";
                        PreparedStatement pstmt = dbconnections.conn.prepareStatement(sql);
                        //pstmt.setInt(1, 5);
                        //pstmt.setBlob(1, new FileInputStream(floorplan), floorplan.length());
                        //pstmt.setBlob(1, new FileInputStream(floorplan));
                        //pstmt.setBlob(1, new FileInputStream(reportsubmitted), reportsubmitted.length());
                        //pstmt.setBlob(1, new FileInputStream(review), review.length());
                        pstmt.executeUpdate(sql);
                        JOptionPane.showMessageDialog(null,"Inserted successfully!");
                        System.out.println(sql);
                    } else if (result == JOptionPane.NO_OPTION) {
                        //do nothing
                    } else if (result == JOptionPane.CANCEL_OPTION) {
                        //do nothing
                    }
                } catch(Exception e){
                //JOptionPane.showMessageDialog(null, e);
                //System.err.println(e);
                    e.printStackTrace();
                }

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

    Default Re: Problem uploading a PDF to MySQL database

    That is not using a PreparedStatement correctly.
    Indeed it is abusing a PreparedStatement.

    Read the link Darryl posted about how to use them, and how to bind variables into a (proper) prepared statement.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    fortwnty420 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: Problem uploading a PDF to MySQL database

    This works for me.. I use the blob database type, however, I treat the file as a byte array and it works fine. I use the code below to store jpg images into the database.

    PreparedStatement s = helper.getConnection().prepareStatement(strSQL);

    s.setString(1,this.first_name());
    s.setString(2,this.last_name());

    // Code etc...

    s.setString(13,this.customer_note());

    try {
    if (!(this.customer_image()==null)){
    s.setBytes(14, this.customer_image()); // Hear customer_images() returns a byte array.
    }
    else{
    s.setBytes(14,null);
    }
    }
    catch(SQLException e){
    System.out.println("Image error");
    }

    s.executeUpdate();

    Hope it helps
    Jeff

  9. #9
    fortwnty420 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: Problem uploading a PDF to MySQL database

    To Write the byte array back to a file...

    bimg = the byte array that you retrieve from the database.
    convert that to a BufferedImage

    InputStream in = new ByteArrayInputStream(bimg);
    BufferedImage image = ImageIO.read(in);


    The, write it out to file, use:

    JFileChooser fc = new JFileChooser();
    fc.setFileFilter(new PictureFilter());
    int result = fc.showSaveDialog(null);
    File file = null;
    if ( result == JFileChooser.APPROVE_OPTION ){
    file = fc.getSelectedFile();

    try
    {
    ImageIO.write(image, "pdf", file);
    }
    catch (IOException err)
    {
    System.out.println("Error:" + err.toString());
    }

    }

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

    Default Re: Problem uploading a PDF to MySQL database

    The OP has a PDF.
    So sticking it in a BufferedImage is no use whatsoever.

    Besides, if you're just going to put it into to a file then take the stream and feed that directly into the OutputStream for wherever you are sending it (File, web client, whatever).
    No need to store the blob in a byte[], or create random other objects from that array. After all, what if this is a few hundred Mb document? You'll not scale terribly well.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Uploading a image to a database via swing
    By hacker9116 in forum JDBC
    Replies: 3
    Last Post: 03-29-2011, 11:35 AM
  2. Problem connecting to mysql database
    By Kelly in forum JDBC
    Replies: 18
    Last Post: 07-22-2010, 06:40 AM
  3. Replies: 3
    Last Post: 02-02-2010, 01:33 PM
  4. problem in connecting to mysql database
    By nancyv in forum Java Servlet
    Replies: 6
    Last Post: 04-02-2008, 11:33 AM
  5. Replies: 0
    Last Post: 03-29-2008, 03:36 PM

Posting Permissions

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