2014-11-01 117 views
1
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); 
    } 
} 

我嘗試插入值到XAMPP Mysql使用Java,但得到了錯誤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如何解決數據包太大的異常XAMPP Mysql

我將my.ini設置1mb更改爲32mb,但錯誤仍在此處。

+1

只需轉到MySQL並執行以下查詢。 set global max_allowed_pa​​cket = 100000; – 2014-11-01 06:24:13

+0

還是同樣的錯誤出現 – 2014-11-01 06:30:52

+1

根據你查詢你的數據包尺寸太大就是 錯誤:com.mysql.jdbc.packetTooBigExcetion:包查詢過大(3548177> 1048576) 所以,請儘量設置數據包大小超過3548177. – 2014-11-01 06:32:44

回答

0

設置全球max_allowed_pa​​cket的用java

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; 
    } 


} 

MySqlSe rviceEx2

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(); 
     } 
    } 
}