Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 主机内连续运行方式_Java_Main - Fatal编程技术网

Java 主机内连续运行方式

Java 主机内连续运行方式,java,main,Java,Main,我有两个整数;启动和停止 初始开始值为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.resultSetToTable

我有两个整数;启动和停止
初始开始值为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循环时,程序崩溃了

空干管

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

}

有两件事:

1) 在这样的while循环中,您可能应该在某个时候调用Thread.sleep()。 2) 在这种情况下,由于您没有在每次调用
Employee\u info
constructor时睡觉并获得新连接,因此您正在创建大量连接,这可能是导致错误的直接原因。 3) 您不应该让
Employee\u 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命名约定。

错误说明了什么?只是在原始问题中添加了错误。如何管理连接?更有可能的是,您在分配连接时出错了。刚刚添加了javaconnect classIs,有没有办法在Upload_Table方法的末尾终止连接?
public class EmployeeDao {

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