Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.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 使用preparestatement时链接到其他应用程序_Java_Mysql_Jdbc - Fatal编程技术网

Java 使用preparestatement时链接到其他应用程序

Java 使用preparestatement时链接到其他应用程序,java,mysql,jdbc,Java,Mysql,Jdbc,所以我使用eclipse和mysql创建了一个登录界面。该接口作为一个shell工作得很好,因此没有连接到数据库,现在我终于将其连接到数据库,但在成功登录后,我遇到了将其链接到另一个应用程序的问题 因此,对于它的外壳,我只给了代码一个用户名/密码组合,以继续进行界面的下一部分,但是如果我想让链接在成功登录后正常工作,我应该将它放在代码中的什么位置 我的代码: package project_files; import javax.swing.JFrame; import javax.swi

所以我使用eclipse和mysql创建了一个登录界面。该接口作为一个shell工作得很好,因此没有连接到数据库,现在我终于将其连接到数据库,但在成功登录后,我遇到了将其链接到另一个应用程序的问题

因此,对于它的外壳,我只给了代码一个用户名/密码组合,以继续进行界面的下一部分,但是如果我想让链接在成功登录后正常工作,我应该将它放在代码中的什么位置

我的代码:

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);



    }
}
我正在使用(目前仍在我的代码中)

将成功登录链接到其他应用程序界面。我能不能 使用


就像我一直在做的那样?添加此代码的正确位置在哪里?第二个应用程序位置是在我的代码开头导入的。

如果您更改
video\u interface
类的构造函数以初始化并显示GUI组件(正如您当前在
login\u sys.initialize
方法中所做的那样),您可以使用

video_interface info = new video_interface()
任何Java程序中都应该只有一个
静态void main(String[]args)
方法


另外,Java通常使用camelcasting(例如,
JFrame
JTextField
),如果您采用这种约定,您的代码将更易于阅读。

很高兴它对@Tengu有所帮助!
video_interface info = new video_interface();
                            video_interface.main(null);
video_interface info = new video_interface()