XAMPP Mysql怎么解决包太大的异常

时间:2014-11-01 06:19:56

标签: java mysql swing

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   Connection connection = null;
   String connectionURL = "jdbc:mysql://localhost:3306/vivek";


    ResultSet rs = null;


    PreparedStatement psmnt = null;

    try {


        Class.forName("com.mysql.jdbc.Driver").newInstance();


        connection = DriverManager.getConnection(connectionURL, "vivek", "voyage88");

        **psmnt = connection.prepareStatement("insert into         save_image(customerid,customername,cnumber,cimage,address,idproof1,idproof2,idc1,idc2,description,ref            name,refcnumber,refaddress,refdescription,dates) "
                + "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");**


        String ci = customerid.getText();
                 psmnt.setString(1, ci);
       String cn= customername.getText();
                 psmnt.setString(2, cn);
       String cc= customernumber.getText();
                 psmnt.setString(3, cc);
       is = new FileInputStream(new File(filename));
       psmnt.setBinaryStream(4, is);

       String ca= address.getText();
                 psmnt.setString(5, ca);        
       String id1 = idproof1.getText();
                 psmnt.setString(6, id1);      
       String id2 = idproof2.getText();
                 psmnt.setString(7, id2);
      String idcc1=id11.getSelectedItem().toString();
                 psmnt.setString(8, idcc1);
      String idcc2=id22.getSelectedItem().toString();
                 psmnt.setString(9, idcc2);  
       String cd= description.getText();
                 psmnt.setString(10, cd);     
       String rn= refname.getText();
                 psmnt.setString(11, rn);      
       String rc= refcontactnumber.getText();
                 psmnt.setString(12, rc);      
       String ra=refaddress.getText();
                 psmnt.setString(13, ra);     
       String rd=refdescription.getText();
                 psmnt.setString(14, rd);  
       String dte=dtess.getText();
                 psmnt.setString(15, dte);





        int s = psmnt.executeUpdate();
        if (s > 0) {
            System.out.println("Records sucessfully Added !");

            JOptionPane.showMessageDialog(rootPane, "Records sucessfully Added");
        } else {
            System.out.println("check given records:Records Not Added");
        }
        connection.close();
        psmnt.close();
    } 
    catch (Exception ex) {
        System.out.println("Found some error : " + ex);
    }
}

我尝试使用Java将值插入XAMPP Mysql,但出现错误com.mysql.jdbc.packetTooBigExcetion:packet for query is too large(3548177>1048576)-you can change this value on theserver by setting the max_allowed_packet' variable

我将my.ini设置1mb更改为32 mb但仍然出现错误。

2 个答案:

答案 0 :(得分:1)

请看这个链接。尝试将您的尺寸增加到1000,这取决于您的尺寸

http://help.hannonhill.com/kb/common-error-messages/mysql-packet-for-query-is-too-large

另请参阅此链接以获取想法

http://dev.mysql.com/doc/refman/4.1/en/packet-too-large.html

答案 1 :(得分:0)

使用java

设置 global max_allowed_pa​​cket
public class CheckMaxPacket {


    public static String checkPacket(String url,String user,String password){
        String key="";
        String value="";
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        try {

            con = DriverManager.getConnection(url, user, password);
            st = con.createStatement();
            rs = st.executeQuery("show variables like 'max_allowed_packet'");
            if (rs.next()) {
                key=rs.getString(1);
                value=rs.getString(2);
            }

            System.out.println("key--->"+key);
            System.out.println("value--->"+Long.parseLong(value));


            rs.close();
            con.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }


        return value;
    }


}

<强> MySqlServiceEx2

public class MySqlServiceEx2 {

    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/db_name";
        String user = "*******";
        String password = "********";
        Connection conn = null;

        try {

            String oldPacketValue=CheckMaxPacket.checkPacket(url,user,password);
            System.out.println("oldPacketValue--->"+oldPacketValue);

            Long oldValue=Long.parseLong(oldPacketValue);
            if(oldValue <= 1024){

                conn = DriverManager.getConnection(url, user, password);
                String querySetLimit = "SET GLOBAL max_allowed_packet=4194304;"; // 104857700  4194304
                Statement stSetLimit = conn.createStatement();
                boolean status = stSetLimit.execute(querySetLimit);
                //System.out.println(status);
                stopMysql();
                startMysql();
                conn.close();

                String newPacketValue=CheckMaxPacket.checkPacket(url,user,password);
                System.out.println("newPacketValue--->"+newPacketValue);

            }else{

                System.out.println("MaxAllowed Packet Is Greater than 1024--->");
            }



        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    private static void startMysql() {
        try {
             Runtime.getRuntime().exec("net START MySQL");
            System.out.println("MySQL server started successfully!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void stopMysql() {
        try {
            Runtime.getRuntime().exec("net STOP MySQL");
            System.out.println("MySQL server stopped successfully!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}