简体   繁体   中英

Linking to another application when using preparestatement

so I am using eclipse and mysql to create a login interface. The interface worked fine as a shell, so without being connected to the databse, and now I finally got it connected to the database but I'm running into the problem of linking it to another application upon successful login.

So for the shell of it I just gave the code a single username/password combo to proceed to the next part of the interface, but if I want to have the link work upon successful login in general, where do I place it in the code?

My code:

package project_files;



import javax.swing.JFrame;
import javax.swing.JToolBar;
import project_files.registration_test;
import project_files.root_login;
import project_files.gui_interface;
import project_files.video_interface;

import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JSeparator;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;

public class login_sys {

    private JFrame frame;
    private JTextField txtUsername;
    private JPasswordField txtPassword;

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

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


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

        JLabel LabelLogin = new JLabel("Login Page");
        LabelLogin.setBounds(209, 12, 100, 15);
        frame.getContentPane().add(LabelLogin);

        JLabel LabelUsername = new JLabel("Username");
        LabelUsername.setBounds(61, 68, 92, 17);
        frame.getContentPane().add(LabelUsername);

        JLabel LabelPassword = new JLabel("Password");
        LabelPassword.setBounds(61, 133, 66, 15);
        frame.getContentPane().add(LabelPassword);

        txtUsername = new JTextField();
        txtUsername.setBounds(244, 68, 124, 19);
        frame.getContentPane().add(txtUsername);
        txtUsername.setColumns(10);

        txtPassword = new JPasswordField();
        txtPassword.setBounds(244, 131, 124, 19);
        frame.getContentPane().add(txtPassword);

        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                try {

                    Class.forName("com.mysql.cj.jdbc.Driver");
                    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/userdatabase", "root", "pass1234");
                            Statement stmt= con.createStatement();
                            String sql = "Select * from user where Email = '" +txtUsername.getText()+"' and Password ='" +txtPassword.getText().toString()+"'";
                            ResultSet rs=stmt.executeQuery(sql);
                            if(rs.next())
                                JOptionPane.showMessageDialog(null, "Login Successfully...");
                            else 
                                JOptionPane.showMessageDialog(null, "Incorrect username and password...");
                            con.close();
                } catch(Exception e) {System.out.print (e);}




                String password = txtPassword.getText();
                String username = txtUsername.getText();

                if(password.contains("pass1234") && username.contains("root")){
                    txtPassword.setText(null);
                    txtUsername.setText(null);
                    root_login info = new root_login();
                    root_login.main(null);
                }
                else
                {

                    if(password.contains("pass1234") && username.contains("john@gmail.com")){
                        txtPassword.setText(null);
                        txtUsername.setText(null);
                        JOptionPane.showMessageDialog(null, "Login Successful", "Login Warning", JOptionPane.WARNING_MESSAGE);
                        video_interface info = new video_interface();
                        video_interface.main(null);
                    }
                    else
                    {

                            JOptionPane.showMessageDialog(null, "Invalid Login Details", "Login Error", JOptionPane.ERROR_MESSAGE);
                            txtPassword.setText(null);
                            txtUsername.setText(null);
                }

            }
            }});
        btnLogin.setBounds(23, 203, 130, 25);
        frame.getContentPane().add(btnLogin);

        JButton btnReset = new JButton("Reset");
        btnReset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                txtUsername.setText(null);
                txtPassword.setText(null);
            }
        });
        btnReset.setBounds(212, 203, 114, 25);
        frame.getContentPane().add(btnReset); 

        JButton btnNewExit = new JButton("Exit");
        btnNewExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                JFrame frmLogin_sys = new JFrame("Exit");
                if (JOptionPane.showConfirmDialog(frmLogin_sys, "Confirm if you want to exit", "Login System",
                JOptionPane.YES_NO_OPTION)== JOptionPane.YES_NO_OPTION) {
                    System.exit(0);
                }
            }
        });
        btnNewExit.setBounds(373, 203, 114, 25);
        frame.getContentPane().add(btnNewExit);

        JSeparator separator = new JSeparator();
        separator.setBounds(12, 175, 499, 2);
        frame.getContentPane().add(separator);

        JSeparator separator_1 = new JSeparator();
        separator_1.setBounds(12, 36, 499, 2);
        frame.getContentPane().add(separator_1);

        JButton btnCreateAccount = new JButton("Create Account");
        btnCreateAccount.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0)


            {
        registration_test info = new registration_test();
        registration_test.main(null);
            }
        });
        btnCreateAccount.setBounds(135, 233, 265, 25);
        frame.getContentPane().add(btnCreateAccount);



    }
}

I was using (and it is still in my code currently)

if(password.contains("pass1234") && username.contains("john@gmail.com")){
                        txtPassword.setText(null);
                        txtUsername.setText(null);
                        JOptionPane.showMessageDialog(null, "Login Successful", "Login Warning", JOptionPane.WARNING_MESSAGE);
                        video_interface info = new video_interface();
                        video_interface.main(null);

to link a successful login to an additional application interface. Can i just use

video_interface info = new video_interface();
                            video_interface.main(null);

as I have been doing? Where is the correct place to add this code? The second application location is imported at the beginning of my code.

If you change the constructor of the video_interface class to initialize and show the GUI components (as you're currently doing in the login_sys.initialize method), you'll be just fine with

video_interface info = new video_interface()

There should only be one static void main(String[] args) method in any Java program.

As a side note, Java conventionally uses camelCasing, (eg JFrame , JTextField ) and if you adopt that convention your code will be easier to read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM