我制作了一个程序,它将(用户名和密码)作为输入,并使用存储在数据库(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