CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  1. #1
    Join Date
    Feb 2012
    Posts
    6

    Post Simple SQL Login to run the main class

    Hey -
    I am currently studying A level computing at sixth form -
    We are coding in java this year and to say the least - im crap.
    Im trying to run a gui from a main class. From this gui they will input there username and password, which will validate with the sql database (All working fine)
    However i need to work a way of getting the login class to give the main class the username of the person that was logged in.
    I can then run various things of there username - whether they are a admin or not, and there current test scores.

    However i cant seem to get the main class to run again when the login has been successful.
    I've tried while and if's and cant think what else will make it happen!

    Here is my code for the main:
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package computinggraphtransformations;
    
    /**
     *
     * @author Joey
     */
    public class Main {
    public static boolean logged = false;
    public static String mainUsername;
    public static boolean execLoop = true;
    
    //if (Logged == true){
    //}
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            //String mainUsername=null;
            //String mainUsername = args[0];
            //System.out.println(args[0]);
            while(execLoop == true){
            
            
            System.out.println("sdfhskdjf");
            if(logged == false){
                NewsqlLogin newsql = new NewsqlLogin();
                System.out.println("logged = false");
                System.out.println(mainUsername);
                            execLoop = false;
    
            }else if(logged == true){
                System.out.println("logged = else");
                System.out.println("debug " + mainUsername);
                
                
            }
        }
        }
        
        public static void newmain() {    
            Main Main = new Main();
            
        }
        
    }

    And for the Login window
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package computinggraphtransformations;
    
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.UnsupportedEncodingException;
    import javax.swing.*;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.sql.*;
    
    
    /**
     *
     * @author Joey
     */
    
    
    
    
    public final class NewsqlLogin extends JFrame{
        private boolean success = false;
        public static String username;
        //public static String password;
    
    
        ///**
         //* @param args the command line arguments
         //*/
        //public static void Main(String[] args) {
            // TODO code application logic here
            //new NewsqlLogin(); //Shows the GUI
        //}
        
        
       // public final class PasswordJDialog extends JDialog {
       //     private final JPanel backgroundPanel;
    
       // }
        
        
         public NewsqlLogin() {
            Components();
            this.pack();
            this.setLocationRelativeTo(null);
            this.setVisible(true);
        }
        
        private void Components() {
            Container c = getContentPane();
            
            //super "Login Window";
            
            //Create the Java components
            JLabel lblUsername = new JLabel("Username: ");
            lblUsername.setHorizontalAlignment(JLabel.CENTER);
            final JTextField txtUsername = new JTextField(10);
            JLabel lblPassword = new JLabel("Password: ");
            lblPassword.setHorizontalAlignment(JLabel.CENTER);
            final JPasswordField txtPassword = new JPasswordField(10);
            txtPassword.setEchoChar('*');
            JButton btnSubmit = new JButton("Submit");
            JButton btnCancel = new JButton("Cancel");
            //btnSubmit.setSize(300, 300);
            
            
            btnSubmit.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    username = txtUsername.getText().trim();
                    String password = null;
                    
                        password = new String(txtPassword.getPassword()).trim();
                     
                        
                        
                    success = submit(username, password);
                    if(success) {
                        JOptionPane.showMessageDialog(null, "Successful login");
                        System.out.println("logged set to true");
                        String id[] = new String[1];
                        id[0]=username;     
                        Main.mainUsername=username;
                        Main.logged = true;
                        Main.main(id);
                        Main.execLoop = true;
                        System.out.println("Logged, Username, Exec Loop");
                        System.out.println(Main.logged);
                        System.out.println(Main.mainUsername);
                        System.out.println(Main.execLoop);
                        System.out.println("New main");
                        
                        new Main();
                        System.out.println("New values");
                        System.out.println(Main.logged);
                        System.out.println(Main.mainUsername);
                        System.out.println(Main.execLoop);
                        
                        
                        System.out.println("Main class started");
                        close();
                    } else {
                        JOptionPane.showMessageDialog(null, "Incorrect username or password");
                    }
                }
    
          
                    
                
            });
    
            btnCancel.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    close();
                }
            });
            
            
            //Add the components to the JFrame
            JPanel cp = new JPanel();
            cp.setPreferredSize(new Dimension(300,100));
            cp.setOpaque(false);
            cp.setLayout(new GridLayout(3, 3, 5, 5));
            cp.add(lblUsername);
            //lblUsername.setSize(300, 300);
            cp.add(txtUsername);
            cp.add(lblPassword);
            cp.add(txtPassword);
            cp.add(btnSubmit);
            cp.add(btnCancel);
            
            c.add(cp) ;
    
          pack();
          setSize( 250, 150 );
          //show();
          
            
            
            
        }
        
        
         protected boolean submit(String username, String password) {
            if(username.length() < 1 || password.length() < 1)
                return false;
            
            return hashAndVerify(username, password);
        }
         
        private boolean hashAndVerify(String username, String password) {
    
    		String query = "SELECT Password FROM userDB WHERE Username = ?";
    		PreparedStatement stmt = DatabaseConnection.getStatement(query);
    		ResultSet rs = null;
    		try {
    			stmt.setString(1, username);
    			rs = stmt.executeQuery();
    			while (rs.next()) {
    				String arr = rs.getString(password);
    				if (password.equals(arr)) {
    					return true;
    				}
    			}
    		} catch (SQLException e1) {
    			e1.printStackTrace();
    		} finally {
    			try {
    				if (rs != null)
    					rs.close();
    				if (stmt != null)
    					stmt.close();
    				DatabaseConnection.closeConnection();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    
    		return false;
    	}
        
        
      /*  private byte[] hash(String password) {
    		MessageDigest sha1 = null;
    		try {
    			sha1 = MessageDigest.getInstance("SHA-1");
    
    		} catch (NoSuchAlgorithmException e) {
    			e.printStackTrace();
    		}
    
    		if (sha1 == null)
    			return null;
    
    		sha1.update(password);
    		return sha1.digest();
    	}*/
        
        
        
        protected void close() {
            this.dispose();
        }
        
        public static void NewsqlLogin() {    
            //new NewsqlLogin();
            
        }
        
    }


    Any help is much appreciated -

    I hope to get all the GUI's to run from the main class, and then the main class can work out what to do (mostly run the main menu) when it receives some sort of response from the other gui classes.

    Thanks

    Joe.


    P.S Have also attached the three .java's as tags dont seem to work
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured