Hi members!
Sorry I need a help with this snippet. I am still workin' on it... the thing is all the three buttons ain't working. And Can anyone tell how to compare the values of username and password against a database in Ms access.
Thanks Here is my code

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class loginForm implements ActionListener{
   private String userid="";
   private String pass="";
   private String url="JDBC:ODBC:Student";
   private Connection cn=null;
   String username="";
   String password="";


   JLabel jlbUserName, jlbPass;
   JTextField jtfUserName, jtfPass;
   JButton jbnOK, jbnCancel, jbnRegister;
   JFrame loginFrame;
   JPanel txt1panel = new JPanel();
   JPanel txt2panel = new JPanel();
   JPanel cmdpanel = new JPanel();

      public static void main(String args[]){
      new loginForm();
      new PatientRegistration();
   }

    private PreparedStatement pstat;
    private ResultSet rs;
        // constructor 
    public  loginForm(){
            // initialize all GUI components
            loginFrame= new JFrame("Login Form");
            loginFrame.getContentPane();

            txt1panel.setLayout(new FlowLayout());
            txt2panel.setLayout(new FlowLayout());
            cmdpanel.setLayout(new FlowLayout());
            //setting the Frame's properties
            loginFrame.setSize(250,400);
            loginFrame.setVisible(true);
            loginFrame.setResizable(false);


            jlbUserName = new JLabel("Username");
            jlbPass = new JLabel("Password");
            jtfUserName = new JTextField(20);
            jtfPass = new JTextField(20);
            jbnOK = new JButton("OK");
            jbnCancel = new JButton("Cancel");
            jbnRegister = new JButton("Register");

            // add GUI components to the JFrame
            txt1panel.add(jlbUserName,BorderLayout.LINE_START);
            txt1panel.add(jtfUserName,BorderLayout.LINE_END);
            loginFrame.add(txt1panel, BorderLayout.NORTH);
            txt2panel.add(jlbPass,BorderLayout.LINE_START);
            txt2panel.add(jtfPass,BorderLayout.LINE_END);
            loginFrame.add(txt2panel, BorderLayout.CENTER);
            cmdpanel.add(jbnOK);
            cmdpanel.add(jbnCancel);
            cmdpanel.add(jbnRegister);
            loginFrame.add(cmdpanel, BorderLayout.PAGE_END);
            loginFrame.pack();
        getConnection();        //Create Connection to the Database
    }

    public Connection getConnection(){

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  

        } 
        catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
        }

        try {
            cn = DriverManager.getConnection(url, userid, pass);
        } 
        catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }

        return cn;
    }

      public void actionPerformed(ActionEvent evt){
          if (evt.getSource()==jbnOK){
                OkButton();       
          }
          else if (evt.getSource()==jbnCancel){
              CancelButton();
          }
          else if (evt.getSource()==jbnRegister){
              RegButton();
          }
      }

    private void CancelButton() {
        jtfUserName.setText("");
        jtfPass.setText("");
    }

    private void OkButton() {

                username    = jtfUserName.getText();
                password = jtfPass.getText();             

 if (username.equals("Abdoul")&& password.equals("toure87")){
     JOptionPane.showMessageDialog(null, "Successful Log in");

                                    new PatientRegistration();

                    }           
 else{
    JOptionPane.showMessageDialog(null, "Log in Failed");
             }
    }

    private void RegButton() {
            username    = jtfUserName.getText();
                password = jtfPass.getText();

        if(username.equals("")){
            JOptionPane.showMessageDialog(null, "Please enter Username.");
        }
         else{

     //create a UserInfo object and pass it to loginForm to save it
         UserInfo user = new UserInfo(username,password);
         loginForm log = new loginForm();
     log.saveDetails(user);
         JOptionPane.showMessageDialog(null, "User Info Saved");
         }
    }
   public void saveDetails (UserInfo user){
        try
        {
    String sql = "INSERT INTO LoginTable VALUES (?,?) ";

            // Create a Preparedstatement
            PreparedStatement ps = cn.prepareStatement(sql);

            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPass());
            ps.executeUpdate();
        }
        catch(Exception e){
            System.out.println(e);
        }

     }


}

Edited 4 Years Ago by mike_2000_17: Fixed formatting

The buttons part is solved, I forgot to add the addActionListeners .
Here the code now, but still need to know how to compare the values against a table in the database Student to which it is connected.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

public class loginForm implements ActionListener{
   private String userid="";
   private String pass="";
   private String url="JDBC:ODBC:Student";
   private Connection cn=null;
   String username="";
   String password="";
   
   
   JLabel jlbUserName, jlbPass;
   JTextField jtfUserName;
   JPasswordField jpfPass;
   JButton jbnOK, jbnCancel, jbnRegister;
   JFrame loginFrame;
   JPanel txt1panel = new JPanel();
   JPanel txt2panel = new JPanel();
   JPanel cmdpanel = new JPanel();

      public static void main(String args[]){
      new loginForm();
   }

    private PreparedStatement pstat;
    private ResultSet rs;
        // constructor 
	public  loginForm(){
            // initialize all GUI components
            loginFrame= new JFrame("Login Form");
            loginFrame.getContentPane();
         
            txt1panel.setLayout(new FlowLayout());
            txt2panel.setLayout(new FlowLayout());
            cmdpanel.setLayout(new FlowLayout());
            //setting the Frame's properties
            loginFrame.setSize(250,400);
            loginFrame.setVisible(true);
            loginFrame.setResizable(false);
            
         
            jlbUserName = new JLabel("Username");
            jlbPass = new JLabel("Password");
            jtfUserName = new JTextField(20);
            jpfPass = new JPasswordField(20);
            jbnOK = new JButton("OK");
            jbnCancel = new JButton("Cancel");
            jbnRegister = new JButton("Register");
            
            // add GUI components to the JFrame
            txt1panel.add(jlbUserName,BorderLayout.LINE_START);
            txt1panel.add(jtfUserName,BorderLayout.LINE_END);
            loginFrame.add(txt1panel, BorderLayout.NORTH);
            txt2panel.add(jlbPass,BorderLayout.LINE_START);
            txt2panel.add(jpfPass,BorderLayout.LINE_END);
            loginFrame.add(txt2panel, BorderLayout.CENTER);
            cmdpanel.add(jbnOK);
            cmdpanel.add(jbnCancel);
            cmdpanel.add(jbnRegister);
            loginFrame.add(cmdpanel, BorderLayout.PAGE_END);
            loginFrame.pack();
            jbnOK.addActionListener(this);
            jbnCancel.addActionListener(this);
            jbnRegister.addActionListener(this);
		getConnection();		//Create Connection to the Database
	}

	public Connection getConnection(){
				
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");	

		} 
		catch(java.lang.ClassNotFoundException e) {
			System.err.print("ClassNotFoundException: ");
			System.err.println(e.getMessage());
		}

		try {
			cn = DriverManager.getConnection(url, userid, pass);
		} 
		catch(SQLException ex) {
			System.err.println("SQLException: " + ex.getMessage());
		}
		
		return cn;
	}
        
      public void actionPerformed(ActionEvent evt){
          if (evt.getSource()==jbnOK){
                OkButton();       
          }
          else if (evt.getSource()==jbnCancel){
              CancelButton();
          }
          else if (evt.getSource()==jbnRegister){
              RegButton();
          }
      }

    private void CancelButton() {
        jtfUserName.setText("");
        jpfPass.setText("");
    }

    private void OkButton() {
   
                username    = jtfUserName.getText();
                password = jpfPass.getText();

 if (username.equals("Abdoul")&& password.equals("toure87")){
     JOptionPane.showMessageDialog(null, "Successful Log in");
                         
                                    new PatientRegistration();
                                    loginFrame.hide();                  
   			        }			
 else{
    JOptionPane.showMessageDialog(null, "Log in Failed");
             }
    }

    private void RegButton() {
      	   	username    = jtfUserName.getText();
                password = jpfPass.getText();
	
	   	if(username.equals("")){
	   		JOptionPane.showMessageDialog(null, "Please enter Username.");
	   	}
         else{
            
	 //create a UserInfo object and pass it to loginForm to save it
         UserInfo user = new UserInfo(username,password);
         loginForm log = new loginForm();
	 log.saveDetails(user);
         JOptionPane.showMessageDialog(null, "User Info Saved");
         }
    }
   public void saveDetails (UserInfo user){
		try
		{
	String sql = "INSERT INTO LoginTable VALUES (?,?) ";

			// Create a Preparedstatement
 			PreparedStatement ps = cn.prepareStatement(sql);
	
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPass());
			ps.executeUpdate();
		}
		catch(Exception e){
			System.out.println(e);
		}
	
     }
    

}
This article has been dead for over six months. Start a new discussion instead.