i am getting a problem with login page with msaccess datbase connectivity pleasehelp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pranayf
    New Member
    • May 2014
    • 2

    i am getting a problem with login page with msaccess datbase connectivity pleasehelp

    here is my code for login page db connectivity issue.... please help me to resolve the probem.

    Code:
    package Log;
    
    
    
    
    
    import java.awt.*;
    
    import javax.swing.*;
    
    import java.awt.event.*;
    import java.sql.*;
    
    public class loginpg extends JFrame implements ActionListener{
    	
    	JPanel panel=new JPanel();
    	JLabel login = new JLabel("LOGIN !");
    	JTextField txtuser=new JTextField(25);
    	JPasswordField txtpass=new JPasswordField(25);
    	JLabel lbluser=new JLabel("USERNAME: ");
    	JLabel lblpass=new JLabel("PASSWORD: ");
    	JButton btnlogin=new JButton(new ImageIcon("button.png"));
    	
    
    	Connection cn;
    	//ResultSet rs;
    	Statement st;
    	PreparedStatement ps;
    	public loginpg(){
    		
    		panel.setLayout (null);
    
    		panel.setBackground(new java.awt.Color(0, 170, 152));
    		//----Adding Components into your Frame
    		panel.add(txtuser);
    		panel.add(txtpass);
    		panel.add(lbluser);
    		panel.add(lblpass);	
    		panel.add(btnlogin);
    		panel.add(login);
    		
    		//-----Setting the location of the components
    		login.setForeground(Color.white);
    		login.setBounds(250,25,250,60);
    		login.setFont(new Font("Serif",Font.BOLD, 24));
    		//login.setHorizontalAlignment(JLabel.CENTER);
    		
    		lbluser.setForeground(Color.white);
    		lblpass.setForeground(Color.white);
    		
    		lbluser.setBounds(150,132,150,10);
    		txtuser.setBounds(230,130,150,20);
    		lblpass.setBounds(150,168,150,10);
    		txtpass.setBounds(230,165,150,20);
    		btnlogin.setBounds(250,220,110,35);
    	
    		
    		//-----Adding the an actionlistener to the buttons
    		btnlogin.addActionListener(this);
    		
            JLabel lbl = new JLabel(new ImageIcon("background.jpg"));
    		
    		lbl.setBounds(0,0,600,400);
    		panel.add(lbl);
    			
    
    		setContentPane(panel);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    
    		
    		//connection
    				
    		try{
    			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    			cn = DriverManager.getConnection("jdbc:odbc:project");
    		}catch(ClassNotFoundException e)  {
     			System.err.println("Failed to load driver");
     			e.printStackTrace();
     	
     		}catch(SQLException e){
     			System.err.println("Unable to connect");
     			e.printStackTrace();
     			
     		}
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		Object source=e.getSource();
    		
    		if(source==btnlogin){
    			try{
    				String str1=txtuser.getText();
    				String str2=txtpass.getText();
    				if((str1.length()==0 || str2.length()==0)){
        					JOptionPane.showMessageDialog(null,"Connot be Process","WARNING",JOptionPane.WARNING_MESSAGE);
        				}
        				else{
    				st=cn.createStatement();
    				String strUser="";
    				String strPass="";
    				
    				ResultSet rs=st.executeQuery("SELECT * FROM tbl_list WHERE UserName='"+str1+"'");
    				while(rs.next()){
    					strUser=rs.getString(1);
    					strPass=rs.getString(4);
    				}
    				
    				
    				
    				
    				
    				st.close();
    		
    				if(strUser.equals(str1)){
    					if(strPass.equals(str2)){
    						
    					
        					
    						JOptionPane.showMessageDialog(null,"Welcome "+txtuser.getText(),"Welcome",JOptionPane.INFORMATION_MESSAGE);
    						
    						
    						
    					
    					}else{
    						JOptionPane.showMessageDialog(null,"Username found but the password is incorrect!","Security Pass",JOptionPane.WARNING_MESSAGE);
    						txtpass.requestFocus(true);
    					}
    				}else{
    					JOptionPane.showMessageDialog(null,"Username is incorrect!","Security Pass",JOptionPane.WARNING_MESSAGE);
    					txtuser.requestFocus(true);
    				}
        				}	
    			}catch(SQLException w){
    			}
    		}
    
    	}
    	
    	public static void main(String[]args){
    		loginpg l=new loginpg();
    		
    		l.setLocation(400,250);
    		l.setSize(600,400);
    		l.setTitle("Log-In");
    		l.setResizable(false);
    		l.setVisible(true);
    		
    	
    	}
    }

    i have attached my whole rar file ... db msaccess db name project package name log , filename loginpg.java
    Last edited by Rabbit; May 5 '14, 04:08 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    You say, you have a problem, but you do not mention which problem.
    On the first view, the code seems OK. Can you please write down the exact error message?
    If it is not a connection problem: can you please write down the table layout and the contained data (demo entries only)?

    Comment

    Working...