有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java My project在NetBeans中运行并连接到数据库(access.mbd),但jar并没有连接到数据库,一直说用户或密码无效

我已经做了一个程序,以(用户名和密码)作为输入,并用存储在数据库(student.mbd)中的用户名和密码进行检查

这很好,并没有问题,但当我尝试运行jar文件时,抛出异常并显示无效的用户名或密码

登录页面代码为:

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends JFrame implements ActionListener {

    Container c = getContentPane();
    private JButton btnLogin,  btnCancel;
    private JLabel lblUName,  lblPasswd;
    private JTextField txtUName;
    private JPasswordField txtPasswd;

    public Login() {
        super("Login ...");
        this.setSize(350, 200);
        this.setLayout(null);
        this.setResizable(false);
        this.setLocation((Settings.getScreenSize().width / 2) - 175, (Settings.getScreenSize().height / 2) - 150);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        lblUName = new JLabel("Username");
        lblPasswd = new JLabel("Password");
        txtUName = new JTextField();
        txtPasswd = new JPasswordField();
        btnLogin = new JButton("Login");
        btnCancel = new JButton("Cancel");
        lblUName.setBounds(50, 40, 140, 25);
        txtUName.setBounds(150, 40, 130, 25);
        lblPasswd.setBounds(50, 80, 140, 25);
        txtPasswd.setBounds(150, 80, 130, 25);
        btnLogin.setBounds(50, 120, 100, 25);
        btnCancel.setBounds(180, 120, 100, 25);
        btnLogin.addActionListener(this);
        btnCancel.addActionListener(this);
        this.add(lblUName);
        this.add(lblPasswd);
        this.add(txtUName);
        this.add(txtPasswd);
        this.add(btnLogin);
        this.add(btnCancel);
    }//constructor closed
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnLogin) {
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con = DriverManager.getConnection("jdbc:odbc:student");
                try {
                    Statement st = con.createStatement();
                    ResultSet rs = st.executeQuery("SELECT * FROM UAD WHERE Username='" + txtUName.getText() +
                            "' and Password='" + txtPasswd.getText() + "'");
                    if (rs.next()) {
                        if (rs.getString(3).equals("Student")) {
                            userMDI frm = new userMDI();
                            frm.setVisible(true);
                        } else {                            
                            new frmAdminMDI().setVisible(true);
                        }
                        this.dispose();
                    }else{
                        JOptionPane.showMessageDialog(null,"Invalid username or password","Invalid",JOptionPane.ERROR_MESSAGE);
                    }
                    con.close();

                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "Invalid username or password", "Invalid", JOptionPane.ERROR_MESSAGE);
                    txtUName.setText("");
                    txtPasswd.setText("");
                }//inner try catch closed
            } catch (Exception x) {
                JOptionPane.showMessageDialog(null, "Unable to connect to the database", "Connection error", JOptionPane.ERROR_MESSAGE);
            }//outer try catch closed
        }//if closed

        if (e.getSource() == btnCancel) {
            System.exit(0);
        }//if closed
    }//actionPerformed() closed
    public static void main(String args[])    {
        new Login().setVisible(true);
    }
}//class closed

请帮助我,因为我必须提交jar文件并将其作为大学项目提交

下载整个项目http://www.4shared.com/rar/9sz2jBtE/CollegeInformationSystem.html


共 (1) 个答案

  1. # 1 楼答案

    我假设您遇到了未处理的异常。检查您的DNS设置。您是否尝试在代码中传递用户名/密码

    Connection con = DriverManager.getConnection("jdbc:odbc:student","username","password");