Shakir Ahamed
Shakir Ahamed

Reputation: 1308

Upload large image to my application in java netbeans

I am creating an application. I want to upload a image and save to my database..when run my application it run successfully for small size images but I can't save large size images to my database..when I going save large size image like 2 MB it prompt an error like

com.mysql.jdbc.PacketTooBigException: Packet for query is too large (2491559 > 1048576). You can change this value on the server by setting the max_allowed_packet' variable.

why comes like this error..how can i solved it This My Code

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

if (evt.getSource() == jButton1)
     {
        int x = 0;
        String s1 = jTextField1.getText().trim();
        String s2 = jTextField2.getText();

        char[] s3 = jPasswordField1.getPassword();
        char[] s4 = jPasswordField2.getPassword(); 
        String s8 = new String(s3);
        String s9 = new String(s4);

        String s5 = jTextField5.getText();
        String s6 = jTextField6.getText();
        String s7 = jTextField7.getText();

        if(s8.equals(s9))
        {
            try
            {
                File image1 = new File(filename);
        FileInputStream fis = new FileInputStream(image1);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        byte buf[] = new byte[1024];
        for (int readNum; (readNum = fis.read(buf)) != -1;) {

            bos.write(buf, 0, readNum);
        }
        cat_image = bos.toByteArray();
        System.out.println(cat_image.length);

            PreparedStatement ps = conn.prepareStatement("insert into reg values(?,?,?,?,?,?,?)");
                ps.setString(1, s1);
                ps.setString(2, s2);
                ps.setString(3, s8);
                ps.setString(4, s5);
                ps.setString(5, s6);
                ps.setString(6, s7);
                ps.setBytes(7, cat_image);
                int rs = ps.executeUpdate();
                x++;
                if (x > 0) 
                {
                    JOptionPane.showMessageDialog(jButton1, "Data Saved Successfully");
                }
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
        else
        {
            JOptionPane.showMessageDialog(jButton1, "Password Dosn't match");
        }

}
 else
{
    jTextField1.setText("");
        jTextField2.setText("");
        jPasswordField1.setText("");
        jPasswordField2.setText("");
        jTextField5.setText("");
        jTextField6.setText("");
        jTextField7.setText("");
}



// TODO add your handling code here:
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

jTextField1.setText("");
        jTextField2.setText("");
        jPasswordField1.setText("");
        jPasswordField2.setText("");
        jTextField5.setText("");
        jTextField6.setText("");
        jTextField7.setText("");



// TODO add your handling code here:
}                                        

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();
    filename = f.getAbsolutePath();
    jTextField3.setText(filename);


       // int returnVal = chooser.showOpenDialog(null);

    //if(returnVal == JFileChooser.APPROVE_OPTION) {

            ImageIcon icon=new ImageIcon(filename);
            jLabel8.setIcon(icon);

    //}




 // TODO add your handling code here:
 }

Upvotes: 0

Views: 1757

Answers (1)

tenorsax
tenorsax

Reputation: 21233

As the exception states the problem is related to the server's max_allowed_packet variable that defines the maximum size of one packet. See Packet Too Large topic that describes how to modify server's configurations files to increase the value of this variable. Also see Using Option Files for more details on configuration files.

Upvotes: 1

Related Questions