如果正确完成其他方法,如何执行方法?

时间:2016-04-27 04:45:26

标签: java boolean

我的名字是安德鲁,我是java的新手。所以,我的问题是,如果我的方法connection.register和connection.connect正确完成,我想打开一个基本窗口。怎么做到这个?我试图用布尔做一些事情,但它没有用。我看到了一种方法:

  

java.sql.Connection.isValid(int timeoutSeconds)   如果连接尚未关闭且仍然有效,则返回true。   驱动程序应提交有关连接的查询或使用其他一些查询   积极验证连接的机制仍然有效   这个方法叫做。驱动程序提交的查询以进行验证   连接应在当前环境中执行   事务。

我认为这是正确的,我的意思是我应该检查连接是否有效,如果是 - 打开基座(窗口)如果没有打印出一些错误或类似的东西。

 package SYSTEM;

    import java.awt.EventQueue;

    import javax.swing.JFrame;

    import java.awt.GridBagLayout;
    import java.awt.Window.Type;

    import javax.swing.JTextField;

    import java.awt.GridBagConstraints;
    import java.awt.Insets;

    import javax.swing.JButton;
    import javax.swing.JTextPane;

    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;

    import javax.swing.JLabel;

    public class LogIn {

        private String userName;
        private String password;
        private String DBAddress;

        private JFrame frame;
        private JTextField textFieldName;
        private JTextField textFieldPassword;
        private JLabel lblUsername;
        private JLabel lblPassword;

        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        LogIn window = new LogIn();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        /**
         * Create the application.
         */
        public LogIn() {
            initialize();
        }

        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            frame = new JFrame();
            frame.setAlwaysOnTop(true);
            frame.setBounds(100, 100, 343, 295);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(null);

            textFieldName = new JTextField();
            textFieldName.setBounds(93, 60, 148, 20);
            frame.getContentPane().add(textFieldName);
            textFieldName.setColumns(10);

            textFieldPassword = new JTextField();
            textFieldPassword.setBounds(93, 115, 148, 20);
            textFieldPassword.setColumns(10);
            frame.getContentPane().add(textFieldPassword);

            JButton btnLogin = new JButton("Login");
            btnLogin.addActionListener(new ActionListener() {

                // po nacisnieciu klawisza biore w variables umieszczam stringi pobrane z tych pól
                public void actionPerformed(ActionEvent arg0) {

                    String userName = textFieldName.getText();
                    String password = textFieldPassword.getText();


                      try {
                          Class.forName("com.mysql.jdbc.Driver");
                      } catch (Exception ex) {
                          // handle the error
                      }
                    Connection con = new Connection();
                    con.register();
                    con.connect(userName, password);

                    Base bas = new Base();
                        bas.main(null);







                }
            });
            btnLogin.setBounds(107, 146, 121, 23);
            frame.getContentPane().add(btnLogin);

            lblUsername = new JLabel("Username");
            lblUsername.setBounds(132, 35, 71, 14);
            frame.getContentPane().add(lblUsername);

            lblPassword = new JLabel("Password");
            lblPassword.setBounds(132, 91, 71, 14);
            frame.getContentPane().add(lblPassword);
        }


    }



 package SYSTEM;

    import java.sql.DriverManager;
    import java.sql.SQLException;

    public class Connection {






        public void register(){


                  try {
                      Class.forName("com.mysql.jdbc.Driver");
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }


        }


        public void connect(String userName, String password){

            java.sql.Connection conn = null;
            String url = "jdbc:mysql://127.0.0.1:3306/cms";
            try {

                DriverManager.getConnection(url,userName,password);


            } catch (SQLException ex) {

                System.out.println("SQLException: " + ex.getMessage());
                System.out.println("SQLState: " + ex.getSQLState());
                System.out.println("VendorError: " + ex.getErrorCode());
            }



        }
    }

    package SYSTEM;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Base {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Base window = new Base();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Base() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 150, 178);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JButton btnNoweZamwienie = new JButton("Nowe zam\u00F3wienie");
        btnNoweZamwienie.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                NewOrder no = new NewOrder();
                no.main(null);
            }
        });
        btnNoweZamwienie.setBounds(8, 22, 129, 23);
        frame.getContentPane().add(btnNoweZamwienie);

        JButton btnListaZamwie = new JButton("Lista zam\u00F3wie\u0144");
        btnListaZamwie.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                List list = new List();
                    list.main(null);
            }
        });
        btnListaZamwie.setBounds(8, 56, 129, 23);
        frame.getContentPane().add(btnListaZamwie);

        JButton btnWyjcie = new JButton("Wyj\u015Bcie");
        btnWyjcie.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                frame.dispose();
            }
        });
        btnWyjcie.setBounds(8, 124, 129, 23);
        frame.getContentPane().add(btnWyjcie);

        JButton button = new JButton("Lista zam\u00F3wie\u0144");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Archives arch = new Archives();
                arch.main(null);
            }
        });
        button.setBounds(8, 90, 129, 23);
        frame.getContentPane().add(button);
    }
}

1 个答案:

答案 0 :(得分:0)

所有框架(或窗口)似乎都是使用自己的main()方法的独立应用程序。这不是必要的。创建一个控制器类作为应用程序,让您的Base,NewOrder,Archives和LogIn窗口扩展JFrame。

例如:

public class Application {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LogIn window = new LogIn();
                    window.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

您的LogIn类现在扩展了JFrame,并且成功注册并连接后会打开一个Base窗口:

public class LogIn extends JFrame {

    private String userName;
    private String password;
    private String DBAddress;

    private JTextField textFieldName;
    private JTextField textFieldPassword;
    private JLabel lblUsername;
    private JLabel lblPassword;

    public LogIn() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {

        setAlwaysOnTop(true);
        setBounds(100, 100, 343, 295);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        textFieldName = new JTextField();
        textFieldName.setBounds(93, 60, 148, 20);
        getContentPane().add(textFieldName);
        textFieldName.setColumns(10);

        textFieldPassword = new JTextField();
        textFieldPassword.setBounds(93, 115, 148, 20);
        textFieldPassword.setColumns(10);
        getContentPane().add(textFieldPassword);

        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() {

            // po nacisnieciu klawisza biore w variables umieszczam stringi
            // pobrane z tych pól
            public void actionPerformed(ActionEvent arg0) {

                String userName = textFieldName.getText();
                String password = textFieldPassword.getText();

                try {
                    Class.forName("com.mysql.jdbc.Driver");
                } catch (Exception ex) {
                    // handle the error
                }
                Connection con = new Connection();

                if (con.register() && con.connect(userName, password)) {
                    Base bas = new Base();
                    bas.setVisible(true);
                    dispose();
                }

            }
        });
        btnLogin.setBounds(107, 146, 121, 23);
        getContentPane().add(btnLogin);

        lblUsername = new JLabel("Username");
        lblUsername.setBounds(132, 35, 71, 14);
        getContentPane().add(lblUsername);

        lblPassword = new JLabel("Password");
        lblPassword.setBounds(132, 91, 71, 14);
        getContentPane().add(lblPassword);
    }

}

这是Connection类。这些方法应该根据成功或失败返回一个布尔值:

public class Connection {

    public boolean register() {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            return true;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean connect(String userName, String password) {

        java.sql.Connection conn = null;
        String url = "jdbc:mysql://127.0.0.1:3306/cms";
        try {
            DriverManager.getConnection(url, userName, password);
            return true;
        } catch (SQLException ex) {

            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
            return false;
        }

    }
}

以此为例,您的Base类现在也扩展了JFrame:

public class Base extends JFrame {

    public Base() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        setBounds(100, 100, 150, 178);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        JButton btnNoweZamwienie = new JButton("Nowe zam\u00F3wienie");
        btnNoweZamwienie.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                NewOrder no = new NewOrder();    //rewrite your code accordingly; make New Order extend JFrame
                no.main(null);
            }
        });
        btnNoweZamwienie.setBounds(8, 22, 129, 23);
        getContentPane().add(btnNoweZamwienie);

        JButton btnListaZamwie = new JButton("Lista zam\u00F3wie\u0144");
        btnListaZamwie.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                List list = new List();   //rewrite your code accordingly; make List extend JFrame
                list.main(null);
            }
        });
        btnListaZamwie.setBounds(8, 56, 129, 23);
        getContentPane().add(btnListaZamwie);

        JButton btnWyjcie = new JButton("Wyj\u015Bcie");
        btnWyjcie.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                dispose();
            }
        });
        btnWyjcie.setBounds(8, 124, 129, 23);
        getContentPane().add(btnWyjcie);

        JButton button = new JButton("Lista zam\u00F3wie\u0144");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Archives arch = new Archives();  //rewrite your code accordingly; make Archives extend JFrame
                arch.main(null);
            }
        });
        button.setBounds(8, 90, 129, 23);
        getContentPane().add(button);
    }
}