0

私は現在GUIプログラムを作成していますが、プログラムをどのようにセットアップするのかがわかりません。そのため、ユーザーがユーザー名とパスワードを入力してからログインボタンをクリックすると、データベースでユーザー名とパスワードがチェックされます。ユーザー名とパスワードがデータベースにある場合、拒否されない場合は受け入れられます。

これは私のウェルカム スクリーンです。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author 
 */
public class Welcome extends JFrame {

    private JButton Existing, New, Exit;
    private JLabel Welcome, Date, Version;
    private JPanel WelcomeP;
    private JMenuItem jmiNew, jmiExisting, jmiExit, jmiAbout;

    public Welcome() {
        //create menu bar
        JMenuBar regMenuBar = new JMenuBar();

        //set menu bar to the applet
        setJMenuBar(regMenuBar);
        //add menu "operation" to menu bar
        JMenu optionsMenu = new JMenu("Options");
        optionsMenu.setMnemonic('O');
        regMenuBar.add(optionsMenu);

        //add menu "help"
        JMenu helpMenu = new JMenu("Help");
        helpMenu.setMnemonic('H');
        helpMenu.add(jmiAbout = new JMenuItem("About", 'A'));
        regMenuBar.add(helpMenu);

        //add menu items with mnemonics to menu "options"
        optionsMenu.add(jmiNew = new JMenuItem("New", 'N'));
        optionsMenu.add(jmiExisting = new JMenuItem("Existing", 'E'));
        optionsMenu.addSeparator();
        optionsMenu.add(jmiExit = new JMenuItem("Exit", 'E'));


        Container c = getContentPane();
        c.setLayout(new BorderLayout());

        WelcomeP = new JPanel();
        WelcomeP.setLayout(new GridLayout(2, 1));


        Welcome = new JLabel("Welcome");
        Date = new JLabel("Date: 01/10/2013");
        Version = new JLabel("Version 0.1");
        Exit = new JButton("Exit");
        Existing = new JButton("Existing User");
        New = new JButton("New User");
        
        WelcomeP.add(Welcome);
        WelcomeP.add(Date);
        WelcomeP.add(Version);
        WelcomeP.add(Existing);
        WelcomeP.add(Exit);
        WelcomeP.add(New);


        Exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        New.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                registerInterface regFace = new registerInterface();
                regFace.setVisible(true);
                Welcome.this.dispose();
                Welcome.this.setVisible(false);

            }
        });
        Existing.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                log login = new log();
                login.setVisible(true);
                login.setSize(500, 300);
                Welcome.this.dispose();
                Welcome.this.setVisible(false);

            }
        });
        jmiExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        jmiNew.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                registerInterface regFace = new registerInterface();
                regFace.setVisible(true);
                Welcome.this.dispose();
                Welcome.this.setVisible(false);

            }
        });
        jmiExisting.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                log shoeFace = new log();
                shoeFace.setVisible(true);
                shoeFace.setSize(500, 300);
                Welcome.this.dispose();
                Welcome.this.setVisible(false);

            }
        });
        //listner for about menuitem
        jmiAbout.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                JOptionPane.showMessageDialog(null, 
                    "Program Dedicated to researchers of eAgriculture"
                                        + "\n Assignment for University", 
                        "About", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        c.add(WelcomeP, BorderLayout.CENTER);

        setSize(500, 300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        Welcome app = new Welcome();
    }
}

既存のユーザーをクリックすると、次のログイン パネルが表示されます。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Login {
//  public static void main(String arg[])
//  {
//      log frame=new log();
//      frame.setSize(500,500);
//      frame.setLocationRelativeTo(null);
//      frame.setVisible(true);     
//  }
}

class log extends JFrame {

    private JTextField jtfUsername, jtfPassword;
    private JButton backButton, loginButton;
    private JMenuItem jmiLogin, jmiBack, jmiHelp, jmiAbout;

    log() {
        //create menu bar
        JMenuBar jmb = new JMenuBar();

        //set menu bar to the applet
        setJMenuBar(jmb);

        //add menu "operation" to menu bar
        JMenu optionsMenu = new JMenu("Options");
        optionsMenu.setMnemonic('O');
        jmb.add(optionsMenu);

        //add menu "help"
        JMenu helpMenu = new JMenu("Help");
        helpMenu.setMnemonic('H');
        helpMenu.add(jmiAbout = new JMenuItem("About", 'A'));
        jmb.add(helpMenu);

        //add menu items with mnemonics to menu "options"
        optionsMenu.add(jmiLogin = new JMenuItem("Login", 'L'));
        optionsMenu.addSeparator();
        optionsMenu.add(jmiBack = new JMenuItem("Back", 'B'));

        //panel p1 to holds text fields
        JPanel p1 = new JPanel(new GridLayout(2, 2));
        p1.add(new JLabel("Username"));
        p1.add(jtfUsername = new JTextField(15));
        p1.add(new JLabel("Password"));
        p1.add(jtfPassword = new JPasswordField(15));

        //panel p2 to holds buttons
        JPanel p2 = new JPanel(new FlowLayout());
        p2.add(backButton = new JButton("Back"));
        p2.add(loginButton = new JButton("Login"));

        //Panel with image??????

        //add panels to frame
        JPanel panel = new JPanel(new GridLayout(2, 1));
        panel.add(p1, BorderLayout.CENTER);
        panel.add(p2, BorderLayout.SOUTH);
        add(panel, BorderLayout.CENTER);
        setTitle("Main Page");


        //listners for exit menuitem and button
        jmiBack.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Welcome welcome = new Welcome();
                welcome.setVisible(true);
                welcome.setSize(500, 500);
                welcome.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                log.this.dispose();
                log.this.setVisible(false);
            }
        });

        backButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Welcome welcome = new Welcome();
                welcome.setVisible(true);
                welcome.setSize(500, 500);
                welcome.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                log.this.dispose();
                log.this.setVisible(false);
            }
        });

        //listner for about menuitem
        jmiAbout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null,
                        "This is the login panel"
                        + "\n Assignment for University",
                        "About", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        //action listeners for Login in button and menu item
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MainMenu mainmenu = new MainMenu();
                mainmenu.setVisible(true);
                mainmenu.setSize(500, 500);
                mainmenu.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                log.this.dispose();
                log.this.setVisible(false);
            }
        });

        jmiLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MainMenu mainmenu = new MainMenu();
                mainmenu.setVisible(true);
                mainmenu.setSize(500, 500);
                mainmenu.setLocationRelativeTo(null);
                registerInterface regFace = new registerInterface();
                regFace.setVisible(false);
                log.this.dispose();
                log.this.setVisible(false);
            }
        });
    }
}

NetBeans で JDBC をセットアップし、ユーザー名とパスワードを含むテーブルを追加し、ユーザー名とパスワードのモックアップでテーブルを埋めました。また、新しいユーザーをクリックすると、新しい情報をデータベースに追加できるようにしたいと考えています。彼らは新しいアカウントを長く使うことができます。

以下は私のJDBCです

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 

public class JDBC { 
    private Connection con = null;
    private String query = null; 
    private ResultSet rs; 
    private String url = "jdbc:derby://localhost:1527/Assignment";    
    private String usrName = "root";
    private String pwd = "root";
    
    public JDBC() throws Exception { 
        try { 
            con = DriverManager.getConnection(url, usrName, pwd); 
            query = "SELECT USERNAME, PASSWORD from root.PERSON";
            PreparedStatement stm = con.prepareStatement(query); 
            rs = stm.executeQuery(); 
            while (rs.next()) { 
                String USER = rs.getString("USERNAME"); 
                String PW = rs.getString("PASSWORD"); 
                
                System.out.println(" USERNAME: " + 
                        USER + " PASSWORD: " + PW); 
            } 
            close(); 
        } catch (Exception e) {           
        }
    }
    private void close() { 
        try { 
            if (rs != null) { 
                rs.close(); 
            } 
            if (con != null) {
                con.close(); 
            }
        } catch (Exception e) { 
        }
    }
    
    public static void main(String[] args) throws Exception { 
        JDBC dao = new JDBC();
    } 
} 
4

3 に答える 3

0

これは、ユーザー名とパスワードのチェックに使用される小さなコードで、mysql データベース用です。

public void check(String username,String password)
{
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","username","password");
        PreparedStatement pt=con.prepareStatement("select username,password from authentication_table where username=?");
        pt.setString(1, username);
        ResultSet rs=pt.executeQuery();
        String orgUname="",orPass="";
        while(rs.next())
        {
            orgUname=rs.getString("username");
            orPass=rs.getString("password");

        }
        if(orPass.equals(password))
        {
            //do something
        }
        else
        {
            //do something
        }
    }
    catch(Exception e)
    {

    }
于 2013-09-21T05:51:32.197 に答える