我正在尝试在学生信息系统中运行一个项目。我实际上已经将数据库从ms访问(accdb)转换为mysql并与我在wampserver中的java应用程序连接。我有两个用户角色,即管理员和学生。但奇怪的是,在将netbeans连接到wampserver之后,我只能通过学生用户详细信息登录,但不能使用管理员详细信息。尝试使用管理员详细信息登录时,会抛出无效的用户名或密码异常。主类登录代码如下所示:
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("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/anu?zeroDateTimeBehavior=convertToNull","root","root");
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
用户表是:
使用printStackTrace();
后抛出的异常java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at frmAdminMDI.<init>(frmAdminMDI.java:40)
at Login.actionPerformed(Login.java:67)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
这里是&#34; frmAdminMDI.java&#34;的代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
public class frmAdminMDI extends JFrame implements ActionListener {
private JMenuBar menubar;
private JMenu mnuAccounts;
private JMenu mnuStdDetails;
private JMenu mnuView;
private JMenu mnuOptions;
private JMenu mnuTools;
private JMenuItem mnuNewUser;
private JMenuItem mnuEditUser;
private JMenuItem mnuDeleteUser;
private JMenuItem mnuNewReg;
private JMenuItem mnuEditReg;
private JMenuItem mnuDelDetails;
private JMenuItem mnuEditMark;
private JMenuItem mnuViewStdDetails;
private JMenuItem mnuMemList;
private JMenuItem mnuMarkList;
private JMenuItem mnuAddSubs,mnuEditSubs,mnuDelSubs;
private JMenuItem mnuSubAlloc;
private JMenuItem mnuCalc, mnuNotepad;
public static JDesktopPane desktop;
public frmAdminMDI() {
super("Student management System");
this.setSize(Settings.getScreenSize().width, Settings.getScreenSize().height - 30);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
setIconImage(new ImageIcon(ClassLoader.getSystemResource("images/appicon.png")).getImage());
setExtendedState(MAXIMIZED_BOTH);
desktop = new JDesktopPane();
menubar = new JMenuBar();
mnuAccounts = new JMenu("Accounts");
mnuStdDetails = new JMenu("Student details");
mnuView = new JMenu("View");
mnuOptions=new JMenu("Options");
mnuTools = new JMenu("Tools");
mnuNewUser = new JMenuItem("New User");
mnuEditUser = new JMenuItem("Edit User");
mnuDeleteUser = new JMenuItem("Delete User");
mnuNewReg = new JMenuItem("New Registration");
mnuEditReg = new JMenuItem("Edit Registration");
mnuDelDetails = new JMenuItem("Delete Details");
mnuEditMark = new JMenuItem("Edit Mark");
mnuViewStdDetails = new JMenuItem("Student Details");
mnuMemList = new JMenuItem("Member List");
mnuMarkList = new JMenuItem("Mark List");
mnuAddSubs=new JMenuItem("Add Subjects");
mnuEditSubs=new JMenuItem("Edit Subjects");
mnuDelSubs=new JMenuItem("Delete Subjects");
mnuSubAlloc=new JMenuItem("Subject Allocation");
mnuNotepad = new JMenuItem("Calculator");
mnuCalc = new JMenuItem("Calculator");
mnuAccounts.add(mnuNewUser);
mnuNewUser.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2, KeyEvent.ALT_MASK));
mnuNewUser.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/user.png")));
mnuAccounts.add(mnuEditUser);
mnuEditUser.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, KeyEvent.ALT_MASK));
mnuEditUser.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/editusr.png")));
mnuAccounts.addSeparator();
mnuAccounts.add(mnuDeleteUser);
mnuDeleteUser.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, KeyEvent.ALT_MASK));
mnuDeleteUser.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/delusr.png")));
mnuStdDetails.add(mnuNewReg);
mnuNewReg.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_MASK));
mnuNewReg.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/newreg.png")));
mnuStdDetails.add(mnuEditReg);
mnuEditReg.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, KeyEvent.CTRL_MASK));
mnuEditReg.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/editreg.png")));
mnuStdDetails.add(mnuDelDetails);
mnuDelDetails.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_MASK));
mnuDelDetails.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/delreg.png")));
mnuStdDetails.addSeparator();
mnuStdDetails.add(mnuEditMark);
mnuEditMark.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.CTRL_MASK));
mnuEditMark.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/editmark.png")));
mnuView.add(mnuMarkList);
mnuMarkList.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, KeyEvent.CTRL_MASK));
mnuMarkList.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/marklist.png")));
mnuView.add(mnuMemList);
mnuMemList.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK));
mnuMemList.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/memlist.png")));
mnuView.add(mnuViewStdDetails);
mnuViewStdDetails.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, KeyEvent.CTRL_MASK));
mnuViewStdDetails.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/stddet.png")));
mnuOptions.add(mnuAddSubs);
mnuOptions.add(mnuEditSubs);
mnuOptions.add(mnuDelSubs);
mnuOptions.addSeparator();
mnuOptions.add(mnuSubAlloc);
mnuTools.add(mnuCalc);
mnuCalc.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK));
mnuCalc.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/calc.png")));
mnuTools.add(mnuNotepad);
mnuNotepad.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, KeyEvent.CTRL_MASK));
mnuNotepad.setIcon(new ImageIcon(ClassLoader.getSystemResource("images/notepad.png")));
menubar.add(mnuAccounts);
menubar.add(mnuStdDetails);
menubar.add(mnuView);
menubar.add(mnuOptions);
menubar.add(mnuTools);
mnuNewUser.addActionListener(this);
mnuEditUser.addActionListener(this);
mnuDeleteUser.addActionListener(this);
mnuNewReg.addActionListener(this);
mnuEditReg.addActionListener(this);
mnuDelDetails.addActionListener(this);
mnuEditMark.addActionListener(this);
mnuMarkList.addActionListener(this);
mnuMemList.addActionListener(this);
mnuViewStdDetails.addActionListener(this);
mnuAddSubs.addActionListener(this);
mnuEditSubs.addActionListener(this);
mnuDelSubs.addActionListener(this);
mnuSubAlloc.addActionListener(this);
mnuCalc.addActionListener(this);
mnuNotepad.addActionListener(this);
this.setJMenuBar(menubar);
this.add(desktop);
}//constructor closed
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == mnuNewUser) {
NewUserReg frm = new NewUserReg();
desktop.add(frm);
frm.setVisible(true);
}
if (e.getSource() == mnuEditUser) {
Edituser frm = new Edituser();
desktop.add(frm);
frm.setVisible(true);
}
if (e.getSource() == mnuDeleteUser) {
Deleteuser frm = new Deleteuser();
desktop.add(frm);
frm.setVisible(true);
}
if (e.getSource() == mnuNewReg) {
NewStdReg frm = new NewStdReg();
desktop.add(frm);
frm.setVisible(true);
}
if (e.getSource() == mnuEditReg) {
EditStdReg frm = new EditStdReg();
desktop.add(frm);
frm.setVisible(true);
}
if (e.getSource() == mnuDelDetails) {
Deletedetails frm = new Deletedetails();
desktop.add(frm);
frm.setVisible(true);
}
if (e.getSource() == mnuMarkList) {
viewMark frm = new viewMark();
desktop.add(frm);
frm.setVisible(true);
}
if (e.getSource() == mnuEditMark) {
EditMark frm = new EditMark();
desktop.add(frm);
frm.setVisible(true);
}
if (e.getSource() == mnuCalc) {
Runtime.getRuntime().exec("calc.exe");
}
if (e.getSource() == mnuNotepad) {
Runtime.getRuntime().exec("notepad.exe");
}
if (e.getSource() == mnuMemList) {
MembersList frm = new MembersList();
desktop.add(frm);
frm.setVisible(true);
}
if (e.getSource() == mnuViewStdDetails) {
User frm = new User();
desktop.add(frm);
frm.setVisible(true);
}
if(e.getSource()==mnuAddSubs){
AddSubjects frm=new AddSubjects();
desktop.add(frm);
frm.setVisible(true);
}
if(e.getSource()==mnuEditSubs){
EditSubjects frm=new EditSubjects();
desktop.add(frm);
frm.setVisible(true);
}
if(e.getSource()==mnuDelSubs){
DelSubjects frm=new DelSubjects();
desktop.add(frm);
frm.setVisible(true);
}
if(e.getSource()==mnuSubAlloc){
SubjectAllocation frm=new SubjectAllocation();
desktop.add(frm);
frm.setVisible(true);
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(new JFrame(), "Error, Cannot load window" + ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
}//try catch closed
}//actionPerformed() closed
} //class closed