将数据库连接到 GUI
Connecting database to GUI
我 运行 在让我的代码与 GUI 一起正常工作方面遇到了一些困难。我创建了一个连接到数据库的简单登录程序,它在 GUI 中让用户输入用户名和密码,然后系统会提示输入数据库以查看用户名和密码是否存在。我创建了一个连接到数据库的 class 和一个执行 SQL 语句的 DAO-class。我还有另外两个 classes,其中一个是接口,另一个包含稍后未实现的方法。
现在,我遇到的问题是将 GUI 连接到我的数据库,这应该通过 DAO-class 完成。我的 GUI 有两个文本字段,应该用来比较用户名和密码,但我不知道该怎么做。另外,我的 DAO-class 并没有完全为它实现,因为它需要一个 int 作为参数。
如果你能帮助我,我将不胜感激。
UserDAO-class:
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-class:
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();
}
}
也许如果你这样做
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!!!");
}
}
我 运行 在让我的代码与 GUI 一起正常工作方面遇到了一些困难。我创建了一个连接到数据库的简单登录程序,它在 GUI 中让用户输入用户名和密码,然后系统会提示输入数据库以查看用户名和密码是否存在。我创建了一个连接到数据库的 class 和一个执行 SQL 语句的 DAO-class。我还有另外两个 classes,其中一个是接口,另一个包含稍后未实现的方法。
现在,我遇到的问题是将 GUI 连接到我的数据库,这应该通过 DAO-class 完成。我的 GUI 有两个文本字段,应该用来比较用户名和密码,但我不知道该怎么做。另外,我的 DAO-class 并没有完全为它实现,因为它需要一个 int 作为参数。
如果你能帮助我,我将不胜感激。
UserDAO-class:
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-class:
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();
}
}
也许如果你这样做
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!!!");
}
}