(ユーザー名とパスワード)を入力として受け取り、データベース(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