0

its a multiple inheritance of 7 classes but in my case I'm only having a problem at one class which will connect you to the data base..

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Login extends JFrame implements ActionListener{
 
 public static void main(String[]args){
   setLogin();
    }
    
    JFrame frame= new JFrame("Administrator Login");
    JLabel lbluser = new JLabel("Username:");
 JLabel lblpass = new JLabel("Password:");
 JTextField txtuser = new JTextField();
 JPasswordField txtpass = new JPasswordField();
 JButton btnok = new JButton(new ImageIcon("keys.gif"));
 JButton btnclose = new JButton(new ImageIcon("exits.png"));
 public String loginname="";
    public String loginpass="";
 Connection cn;
 Statement st;
 ResultSet rs;
 PreparedStatement ps;
 public String user="";
 public String pass="";
 public int cnt;
 int dialogtype = JOptionPane.PLAIN_MESSAGE;
    String dialogmessage;
    String dialogs;
 
 
   public static void setLogin(){
 Login p1= new Login();
    p1.setSize(250,140);
    p1.setLocation(300,300);
    p1.setVisible(true);
    p1.setResizable(false);
 }
 
 
    public Login() {
     super("Administrator Login");
     JPanel pane= new JPanel();
     pane.setLayout(null);
     
     pane.setBackground(Color.black);
     lbluser.setForeground(Color.white);
     lblpass.setForeground(Color.white);
     
     pane.add(lbluser);
     lbluser.setBounds(5,5,100,20);
     pane.add(txtuser);
     txtuser.setBounds(110,5,100,20);
    
     pane.add(lblpass);
     lblpass.setBounds(5,30,100,20);
     pane.add(txtpass);
     txtpass.setBounds(110,30,100,20);
    
     pane.add(btnok);
     btnok.setBounds(120,65,40,35);
     btnok.addActionListener(this);
     
     pane.add(btnclose);
     btnclose.setBounds(155,65,40,35);
     btnclose.addActionListener(this);
     
     btnok.setToolTipText("Log-in");
  btnclose.setToolTipText("Exit");
  
     setContentPane(pane);
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     pane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), ""));
     
     try{
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   cn = DriverManager.getConnection("jdbc:odbc:empPay");
  }catch(ClassNotFoundException e)  {
    JOptionPane.showMessageDialog(null,"unable to load driver");
    System.exit(0);
   }catch(SQLException e){
    JOptionPane.showMessageDialog(null,"unable to connect");
   System.exit(0);
  }
    }
    public void actionPerformed(ActionEvent e){
     
     Object source=e.getSource();
      if(source==btnok)
        {

      try{
       String str1=txtuser.getText();
    String str2=txtpass.getText();
        if((str1.length()==0 || str2.length()==0)){
         JOptionPane.showMessageDialog(null,"Some Fields are empty","WARNING",JOptionPane.WARNING_MESSAGE);
        }
        else{
        st=cn.createStatement();
        rs=st.executeQuery("SELECT * from Login Where username like'" +txtuser.getText() + "'AND password='"+txtpass.getText()+"'");
       
       while(rs.next()){
        loginname=rs.getString("Username");
        loginpass=rs.getString("Password");
       }
       if((loginname.equalsIgnoreCase(txtuser.getText()))&&(loginpass.equalsIgnoreCase(txtpass.getText())))
       { 
        
        
         MainMenu menu = new MainMenu();
                   menu.setMain();
                   
        dialogmessage = "Welcome - "+loginname;
                    dialogtype = JOptionPane.INFORMATION_MESSAGE;
                    JOptionPane.showMessageDialog((Component)null, dialogmessage, dialogs, dialogtype);
                    txtuser.setText("");
                    txtpass.setText("");
              dispose();
                  
       }
        else 
                {
                    JOptionPane.showMessageDialog(null, "INVALID ID OR PASSWORD!","WARNING!!",JOptionPane.WARNING_MESSAGE);
                    cnt=cnt+1;
                    txtuser.setText("");
                    txtpass.setText("");
                }  
       
       if(cnt==3){
        frame.dispose();
       }
        }
       }catch(SQLException c){
           System.out.print(c.getMessage()); 
             }
            
        }
        

        else
        {
              System.exit(0);
        }
      if(source==btnclose){
       dispose();
      }
    }
}

I will end up having A "Unable to load Driver" , i use Xampp and querybrowser to use the SQL. but for some reason i can't connect is there something I'm missing?

  • 1
    which java version are you using? Have you seen this post http://stackoverflow.com/questions/25892923/exception-on-loading-jdbc-odbc-driver What is the exact error that you are getting? – Yan Mar 02 '16 at 03:10
  • I'm using version 8, the problem I'm having is It's not connecting to the database but instead its displaying the "Exception e" – zebasten yunero T. morphling Mar 02 '16 at 03:24
  • What if you do instead of OptionPane.showMessageDialog(null,"unable to load driver"); OptionPane.showMessageDialog(null,e.getMessage()); that will give you the exact error. – Yan Mar 02 '16 at 03:27
  • check out this post. http://stackoverflow.com/questions/22984438/java-lang-classnotfoundexception-sun-jdbc-odbc-jdbcodbcdriver-exception-occurin Could be related to Java 8 – Yan Mar 02 '16 at 03:30
  • The error I'm getting is "sun.jdbc.odbc.JdbcOdbcDriver" – zebasten yunero T. morphling Mar 02 '16 at 03:34
  • I can't say for sure but as the above post mentions that for Java 8 you cannot use the JDBC-ODBC Bridge because it has been removed. You will need to use something like UCanAccess instead. – Yan Mar 02 '16 at 03:36
  • what would i need to Put there?.. as a bridge??.. like I can access? – zebasten yunero T. morphling Mar 02 '16 at 03:48
  • oh yeah .. i was wrong sorry.. The problem is i can't connect to the SQL – zebasten yunero T. morphling Mar 02 '16 at 03:50
  • Truthfully i am not sure. You might want to look into UCanAccess library http://ucanaccess.sourceforge.net/site.html I have never used it before though. – Yan Mar 02 '16 at 03:54

0 Answers0