在使我的代码与GUI一起正常工作时,我遇到了一些困难。我创建了一个连接到数据库的简单登录程序,该程序在GUI中允许用户输入用户名和密码,然后提示用户输入数据库以查看用户名和密码是否存在。我创建了一个连接数据库的类和一个执行SQL语句的DAO类。我还有另外两个类,其中一个是接口,一个包含未实现的方法,供以后使用。
现在,我遇到的问题是将GUI连接到我的数据库,这应该通过DAO类完成。我的GUI有两个文本字段,应该使用这两个文本字段来比较用户名和密码,但是我不知道该怎么做。另外,我的DAO类也没有完全实现,因为它使用int作为参数。
如果您能帮助我,我会很感激的。
UserDAO类:
public class UserDAO implements IDao<User> {
DemoDB DemoDBSingleton = null;
public UserDAO() {
DemoDBSingleton = DemoDB.getInstance();
}
@Override
public User get(int id) throws NoSuchElementException {
User user = null;
try {
ResultSet resultSet = DemoDBSingleton
.excecuteQuery("SELECT user_name, password FROM users WHERE id=" + id);
if (!resultSet.next())
throw new NoSuchElementException("The user " + id + " doesn't exist in database");
else
DemoDBSingleton.close();
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
}
GUI类:
import dao.UserDAO;
public class LoginGUI {
DemoDB DemoDBSingleton = null;
ResultSet rs;
UserDAO userDao = new UserDAO();
JFrame loginFrame = new JFrame("Welcome to your app");
JFrame f = new JFrame("User Login");
JLabel l = new JLabel("Användarnamn:");
JLabel l1 = new JLabel("Lösenord:");
JTextField t = new JTextField(10);
JTextField t1 = new JTextField(10);
JButton b = new JButton("Login");
public LoginGUI() {
DemoDBSingleton = DemoDB.getInstance();
frame();
}
private void frame() {
f.setSize(800, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
loginFrame.setSize(800, 600);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginFrame.setVisible(false);
JPanel p = new JPanel();
p.setLayout(null);
l.setFont(new Font("Tahoma", Font.PLAIN, 21));
l.setBounds(102, 173, 169, 41);
p.add(l);
t.setHorizontalAlignment(SwingConstants.CENTER);
t.setFont(new Font("Tahoma", Font.PLAIN, 16));
t.setBounds(303, 177, 169, 41);
p.add(t);
l1.setFont(new Font("Tahoma", Font.PLAIN, 21));
l1.setBounds(102, 290, 93, 26);
p.add(l1);
t1.setHorizontalAlignment(SwingConstants.CENTER);
t1.setFont(new Font("Tahoma", Font.PLAIN, 16));
t1.setBounds(303, 287, 169, 41);
p.add(t1);
b.setBounds(334, 375, 110, 52);
p.add(b);
f.getContentPane().add(p);
}
private class ClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
UserDAO userDao = new UserDAO();
}
}
public static void main(String[] args) {
new LoginGUI();
}
}
答案 0 :(得分:0)
也许您是用这种方式
public class UserDAO implements IDao<User> {
DemoDB DemoDBSingleton = null;
public UserDAO() {
DemoDBSingleton = DemoDB.getInstance();
}
@Override
public boolean login(String userName, String password) throws NoSuchElementException {
boolean login = false;
try {
ResultSet resultSet = DemoDBSingleton
.excecuteQuery("SELECT user_name, password FROM users WHERE user_name='" + userName + "' AND password = '" + password + "'");
if (resultSet.next())
login = true;
} catch (SQLException e) {
e.printStackTrace();
}
DemoDBSingleton.close();
return login;
}
}
并更正您的文本字段
JTextField txtUserName = new JTextField(10);
JPasswordField txtPassword = new JPasswordField(10);
和点击事件
private class ClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
UserDAO userDao = new UserDAO();
if (userDao.login(txtUserName.getText(), txtPassword.getText())) {
// here you redirect to your main window
}
else
JOptionPane.showMessageDialog(null, "Incorrect user or password!!!");
}
}