我有两个整数;开始和停止
最初的启动是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;
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;
}
}
}
员工_INFO类打电话给Javaconnect类以建立连接;
public Employee_info() {
initComponents();
conn=javaconnect.ConnecrDb();
Update_table();
}
有几件事:
1)在这样的循环中,您可能应该在某个时候调用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命名约定。