mardi 15 décembre 2015

Login System i dont know hot to do it

I working on java mini game with swing so its look like a program(Cause i just learn java + database).Now i have login system but i don't know how to do it: *when i login i stay logged in until o do not close program *because i want to make a char for my account and save character stats to my account id so ----> table account ID: 2 Account:Yolo Password:Swag -----------------> table Character ID:"my id " Char name what i want etc so i want to ask how to get current accountid and insert it to another table with char stats?i learning so don't blame me:)


`

static Connection connection = null;
Statement stmt = null;

    public JFrame LoginF;
    public JLabel UsernameL;
    public JLabel PasswordL;
    public JButton LoginB;
    public JTextField User;
    public JPasswordField Pass;


    public static void main(String args[]){

        Login log = new Login();
        log.Swing();
        connection=LoginIn.connector();
    }

    public void Swing(){
        LoginF = new JFrame("Game");
        LoginF.setSize(400,400);
        LoginF.getContentPane().setLayout(null);

        User = new JTextField();
        User.setBounds(219, 63, 86, 20);
        LoginF.getContentPane().add(User);
        User.setColumns(10);

        Pass = new JPasswordField();
        Pass.setBounds(219, 122, 86, 20);
        LoginF.getContentPane().add(Pass);

        JLabel UsernameL = new JLabel("Username");
        UsernameL.setBounds(65, 66, 69, 14);
        LoginF.getContentPane().add(UsernameL);

        JLabel PasswordL = new JLabel("Password");
        PasswordL.setBounds(65, 125, 69, 14);
        LoginF.getContentPane().add(PasswordL);

        JButton LoginB = new JButton("Login");
        LoginB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try{
                    String query="select * from AccountAdatok where Username=? and Password=?";
                    PreparedStatement pst=connection.prepareStatement(query);
                    pst.setString(1, User.getText());
                    pst.setString(2, Pass.getText());

                    ResultSet rs=pst.executeQuery();
                    int count=0;
                    while(rs.next()){
                        count=count+1;
                    }
                    if(count ==1)
                    {
                        JOptionPane.showMessageDialog(null, " Correct!");
                        LoginF.dispose();
                        MakeCharacter.main(arg0);
                    }
                    else if (count>1)
                    {
                        JOptionPane.showMessageDialog(null, " Cannot Duplicate!");
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null, " InCorret!");
                    }
                    rs.close();
                    pst.close();
                }catch(Exception e){
                    JOptionPane.showMessageDialog(null, e);
                }
                finally{
                    try{

                    }catch(Exception e){
                        JOptionPane.showMessageDialog(null, e);
                    }
                }
            }
        });
        LoginB.setBounds(145, 201, 89, 23);
        LoginF.getContentPane().add(LoginB);

        LoginF.setVisible(true);
    }

}`

Aucun commentaire:

Enregistrer un commentaire