2021年1月2日星期六

How to fix MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"?

My java swing program have this error:

 errorcom.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"  

I think maybe it was because I am using documentListener. But I can't figure out how to fix it. I am stuck with this error for few days now :(

So this program uses a virtualkeyboard to search mysql database for names in the editable comboBox.

 import java.sql.Connection;   import java.sql.DriverManager;   import java.sql.ResultSet;   import java.sql.Statement;   import java.util.ArrayList;   import javax.swing.DefaultComboBoxModel;   import javax.swing.JTextField;   import javax.swing.SwingUtilities;   import javax.swing.event.DocumentEvent;   import javax.swing.event.DocumentListener;   import virtualkeyboard.gui.DialogVirtualKeyboardReal;       public class test3 extends javax.swing.JFrame {     public test3() {      initComponents();        JTextField textfield = (JTextField) comboBox.getEditor().getEditorComponent();         textfield.getDocument().addDocumentListener(new DocumentListener() {          @Override          public void insertUpdate(DocumentEvent e) {               Runnable doAssist = new Runnable() {                      @Override                      public void run() {              comboFilter(textfield.getText());                      } };                  SwingUtilities.invokeLater(doAssist);          }            @Override          public void removeUpdate(DocumentEvent e) {              Runnable doAssist = new Runnable() {                      @Override                      public void run() {              comboFilter(textfield.getText());                      } };                  SwingUtilities.invokeLater(doAssist);          }            @Override          public void changedUpdate(DocumentEvent e) {              // plain text components dont fire this          }      });     }        public void comboFilter(String enteredText) {      java.util.List<String> filterArray = new ArrayList<String>();        String lname = "";      String fname = "";      String mi = "";      String id = "";        try {            String str = "SELECT * FROM patient_record WHERE firstname  LIKE '" + enteredText + "%' OR lastname  LIKE '" + enteredText + "%' OR patient_id  LIKE '" + enteredText + "%'";          Class.forName("com.mysql.jdbc.Driver");          String url = "jdbc:mysql://localhost/patient";          Connection con = DriverManager.getConnection(url, "root", "");            Statement stmt = con.createStatement();          ResultSet rs = stmt.executeQuery(str);          while (rs.next()) {              lname = rs.getString("lastname");              fname = rs.getString("firstname");              mi = rs.getString("middlename");              id = rs.getString("patient_id");              String str1 = lname + ", " + fname + " " + mi;//+". \t"+id;              filterArray.add(str1);            }        } catch (Exception ex) {          System.out.println("error" + ex);      }        if (filterArray.size() > 0) {          comboBox.setModel(new DefaultComboBoxModel(filterArray.toArray()));          comboBox.setSelectedItem(enteredText);          comboBox.showPopup();      } else {          comboBox.hidePopup();      }     }        public void jButton1ActionPerfomed(java.awt.event.ActionEvent evt){    test3 r =this;     JTextField textfield = (JTextField)     comboBox.getEditor().getEditorComponent();      DialogVirtualKeyboardReal dlg = new DialogVirtualKeyboardReal(r, true, textfield);  }  

I already tried putting this after the try-catch for it to close the connection:

finally {   if (con != null) {       try {           // finally lets close the connection (event in the event of a exception)           con.close();       } catch (SQLException ex) {           Logger.getLogger(test3.class.getName()).log(Level.SEVERE, null, ex);       }    }  }  

But the program hangs or freezes. What should I do? Please help me out. Thank you

https://stackoverflow.com/questions/65546529/how-to-fix-mysqlnontransientconnectionexception-data-source-rejected-establishm January 03, 2021 at 12:03PM

没有评论:

发表评论