我想做一個登錄程序,我需要交叉引用GUI中的用戶名/密碼到數據庫,我目前有代碼,但它只是說「不正確的用戶名/密碼! 「,與數據庫中的人數相同。任何幫助實際工作都會很好。我的代碼如下。交叉檢查信息對數據庫
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Login extends JFrame {
JTextField _username = new JTextField(10);
JPasswordField _password = new JPasswordField(10);
JButton _login = new JButton("Login");
JButton _exit = new JButton("Exit");
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = (int)screenSize.getHeight();
int screenWidth = (int)screenSize.getWidth();
public Login() {
super("Login to panel");
setLayout(new GridLayout(3,2,1,1));
add(new JLabel("Username:"));
add(_username);
add(new JLabel("Password:"));
add(_password);
add(_login);
_login.addActionListener(new LoginListener());
add(_exit);
_exit.addActionListener(new ExitListener());
setSize(250, 100);
//setLocation(screenWidth/3, screenHeight/3);
setLocation(500, 400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Login();
}
public class LoginListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Connection con;
Statement stmt;
ResultSet rs;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:collegesys","root","0blivi0n");
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("SELECT * FROM main");
PreparedStatement loginTime = con.prepareStatement("UPDATE `main` WHERE ID = ?");
while(rs.next()) {
if(_username.getText() == rs.getObject("username") && _password.getText() == rs.getObject("password")) {
dispose();
new Panel();
} else {
JOptionPane.showMessageDialog(null, "Incorrect Username/Password!");
}
}
} catch(SQLException sqle) {
sqle.printStackTrace();
} catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
}
public class ExitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(0);
}
}
}
這不會幫助我的程序功能嗎? –
@NathanKreider怪異的,因爲它是你如何比較用戶名和密碼,它不會與==。沒有更多的信息,我們可以做的不多。當你已經有了你可以選擇的數據時,通過整個用戶數據庫也是非常低效的。 –