我有两个整数;开始和停止
最初启动为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();
}
});
}
错误;
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 :(得分: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通常是一个二手的,呃。
5)遵循Java命名约定。