Java,在swing应用程序中保持全局数据库会话

Java,在swing应用程序中保持全局数据库会话,java,database,swing,global,cardlayout,Java,Database,Swing,Global,Cardlayout,嗯。我有一个包含两个容器的主机。一个包含类似于向导的按钮,另一个包含contentPane。我有几个类Window1、Window2等,我在大型机类中实例化它们,然后通过一个cardLayout进行浏览,该cardLayout决定在contentPane中可以看到哪个窗口类 每个窗口类都需要一个到数据库的连接。目前,我分别在window类中实例化了一个数据库连接,但我需要某种形式的全局会话,从我通过Window1类(也称为登录类)的那一刻起,一直连接到我关闭应用程序,因此当我进入其他window

嗯。我有一个包含两个容器的主机。一个包含类似于向导的按钮,另一个包含contentPane。我有几个类Window1、Window2等,我在大型机类中实例化它们,然后通过一个cardLayout进行浏览,该cardLayout决定在contentPane中可以看到哪个窗口类

每个窗口类都需要一个到数据库的连接。目前,我分别在window类中实例化了一个数据库连接,但我需要某种形式的全局会话,从我通过Window1类(也称为登录类)的那一刻起,一直连接到我关闭应用程序,因此当我进入其他window类时,我可以使用此会话读取和写入数据库

我的大型机类:

public class MainFrame {
private static void createAndShowGUI() {
    JFrame frame = new JFrame("Stackoverflowquestion");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);

    final JPanel contentPane = new JPanel();
    contentPane.setLayout(new CardLayout(5, 5));

    Database db = new Database();
    //Trying to make some kind of global database instance but fails

    Window1 win1 = new Window1();
    contentPane.add(win1);
    Window2 win2 = new Window2();
    contentPane.add(win2);
    Window3 win3 = new Window3();
    contentPane.add(win3);
    Window4 win4 = new Window4();
    contentPane.add(win4);
    Window5 win5 = new Window5();
    contentPane.add(win5);
    Window6 win6 = new Window6();
    contentPane.add(win6);

    JPanel buttonPanel = new JPanel(); 
    final JButton previousButton = new JButton("< PREVIOUS");
    previousButton.setEnabled(false);
    final JButton nextButton = new JButton("NEXT >");
    final JButton cancelButton = new JButton("CANCEL");
    buttonPanel.add(cancelButton);
    buttonPanel.add(previousButton);
    buttonPanel.add(nextButton);            

    nextButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            nextButton.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            Verifiable verifiable = null;
            Component[] contents = contentPane.getComponents();
            for(Component component : contents) {
                if(component.isVisible() && component instanceof Verifiable) {
                    verifiable = (Verifiable)component;
                }
            }
            if(verifiable != null && verifiable.isDataValid()) {
                CardLayout cardLayout = (CardLayout) contentPane.getLayout();
                cardLayout.next(contentPane); 
                previousButton.setEnabled(true);
                nextButton.setCursor(Cursor.getDefaultCursor()); 

            }
        }
    });

    cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            System.exit(0);
              //Should close the database session
        }

    });

    frame.add(contentPane);
    frame.add(buttonPanel, BorderLayout.PAGE_END);
    frame.setSize(400, 400);
    frame.setVisible(true);
}
    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
} 

}我会使用单例模式。它基本上允许你有一个对象的实例,你可以在任何地方得到它。在此处了解更多信息:

在我下面的示例中,您可以使用一个数据库连接来创建对数据库的任何调用。我在一个我目前正在做的项目中做着完全相同的事情。它工作得很好:)

下面是一个例子:

public class DatabaseConnection {

  private static DatabaseConnection instance; //note this is static

  private DatabaseConnection() { //note this is private
  }

  public static DatabaseConnection getInstance() { //note this is static
    if (instance == null) {
      instance = new DatabaseConnection();
    }
    return instance;
  }
你也可以尝试维基百科所说的传统方式(在看到这一点后,我想从现在开始我会这样做)


谢谢你的回答。我读过一些关于单例模式的书,但我不太了解它。你介意展示一些我如何在当前应用程序中实现它的代码吗?我已经用一个使用singleton模式的示例类更新了我的答案。基本上,构造函数是私有的,因此唯一可以创建它的类就是它自己。它还有一个名为
instance
的全局变量,它属于自己的类型。然后使用下面的方法
publicstaticconnectiongetinstance()
,其中返回变量
instance
(如果为null,首先说
instance=newconnection();
)。通过这种方式,您可以有一个到数据库的连接,并且可以随时随地获取它,而无需将它从一个连接传递到另一个连接。这有意义吗?您的数据库逻辑应该与GUI完全分离。你甚至不应该在问这个问题时给我们你的GUI代码:-)单例模式会很好地工作。我想补充一点,您可能想查看commons dbcp,它将为您处理大多数数据库连接内容(还使用commons池,它将为您提供连接池)Michael:嗯,是的,我知道。但是,我发现很难将当前代码迁移到某种MVC体系结构中。如果你有任何建议,我很乐意接受。@TobiasJohansson,我很高兴它对你有用。如果答案足够的话。请通过勾选箭头下的复选标记来接受答案,这样将来的人就会知道这就是你要找的。如果您对此还有其他问题,我很乐意在另一个问题上帮助您:)
public class DatabaseConnection {

  private static DatabaseConnection instance; //note this is static

  private DatabaseConnection() { //note this is private
  }

  public static DatabaseConnection getInstance() { //note this is static
    if (instance == null) {
      instance = new DatabaseConnection();
    }
    return instance;
  }
public class DatabaseConnection {
  private static final DatabaseConnection instance = new DatabaseConnection();

  // Private constructor prevents instantiation from other classes
  private DatabaseConnection() { }

  public static DatabaseConnection getInstance() {
    return instance;
  }
}