2014-01-27 53 views
0

我有兩個整數;啓動和停止
最初開始爲0,停止爲1.一旦用戶關閉窗口,開始變爲1.連續運行內部主要方法

我有一個方法可以更新我的JTable;

private void Update_table(){ 


try{ 
    String sql ="select * from orders "; 
    pst=conn.prepareStatement(sql); 
    rs=pst.executeQuery(); 
    Table_Employee.setModel(DbUtils.resultSetToTableModel(rs)); 


} 
catch(Exception e){ 
    JOptionPane.showMessageDialog(null, e); 

} 

} 

我想連續更新表,但當我在void main方法內部放置一個while循環時,程序崩潰;

void main;

public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 

      //Update_table(); 
    while(start<stop) 
     new Employee_info().Update_table(); 


    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(Employee_info.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(Employee_info.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(Employee_info.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(Employee_info.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new Employee_info().setVisible(true); 
     // while(1<2) 
      // rh.Update_table(); 
      // Update_table(); 
     } 
    }); 
} 

該erro;

com.mysql.jdbc.exceptions.jdbc4.MySQL SyntaxErrorException: User 12345 already has more than 'max_user_connections' active connections 

其中12345是連接到數據庫的用戶名,是因爲我在不同的類中登錄數據庫並運行查詢嗎?

連接類;

import java.sql.*; 
import javax.swing.*; 

public class javaconnect { 
Connection conn=null; 

    public static Connection ConnecrDb(){ 
     try{ 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection conn = DriverManager.getConnection("jdbc:mysql://XXX.com:3306/XXX_pizza","12345 ","XXXX"); 
    // JOptionPane.showMessageDialog(null, "You got connected"); 
       return conn; 

    }catch(ClassNotFoundException | SQLException e){ 
     JOptionPane.showMessageDialog(null, e); 
     return null; 
}  

} 
} 

Employee_Info類調用javaconnect類來建立連接;

public Employee_info() { 
    initComponents(); 
    conn=javaconnect.ConnecrDb(); 
    Update_table(); 

} 
+0

錯誤說的是什麼? – BitNinja

+0

剛添加錯誤的原始問題。 – raklar

+0

如何管理連接?更可能的是,你在分配連接時做錯了什麼。 –

回答

0

有幾件事情:

1)在這樣的一個while循環,你應該在某個時候調用Thread.sleep()方法。 2)在這種情況下,由於您每次撥打Employee_info構造函數時都沒有睡覺並獲得新連接,所以您正在創建大量連接,這可能是導致錯誤的直接原因。 3)您不應該有像Employee_info這樣的業務對象進行連接。相反,你應該有之間(通常稱爲數據訪問層)中的一層,像這樣的東西:

public class EmployeeDao { 

    public Employee_info getEmployeeInfo(){ 
     Connection conn = getConnection(); 
     //do something with the connection, construct employee info 
     return employeeInfo 
    } 
} 

4)你應該使用連接池,而不是用手實例連接。 Commons-dbcp是一個常用的erm。

5)遵循Java命名約定。

+1

...並且不要忘記*關閉*連接... – KarlP