连接到数据库失败

时间:2016-03-02 03:05:52

标签: java mysql

它是7个类的多重继承,但在我的情况下,我只在一个类中遇到问题,这会将你连接到数据库..



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();
    		}
    }
}




我最终会得到A"无法加载驱动程序" ,我使用Xampp和querybrowser来使用SQL。但由于某种原因,我无法连接是否有我失踪的东西?

0 个答案:

没有答案