第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將數(shù)據(jù)庫連接到 GUI

將數(shù)據(jù)庫連接到 GUI

守候你守候我 2024-01-05 14:46:01
我在讓我的代碼與 GUI 一起正常工作時遇到了一些困難。我創(chuàng)建了一個連接到數(shù)據(jù)庫的簡單登錄程序,該程序在 GUI 中允許用戶輸入用戶名和密碼,然后提示數(shù)據(jù)庫以查看用戶名和密碼是否存在。我創(chuàng)建了一個連接到數(shù)據(jù)庫的類和一個執(zhí)行 SQL 語句的 DAO 類。我還有兩個類,其中一個是接口,另一個包含稍后未實現(xiàn)的方法。現(xiàn)在,我遇到的問題是將 GUI 連接到我的數(shù)據(jù)庫,這應(yīng)該通過 DAO 類來完成。我的 GUI 有兩個文本字段,應(yīng)該用來比較用戶名和密碼,但我不知道該怎么做。另外,我的 DAO 類并未完全實現(xiàn)它,因為它采用 int 作為參數(shù)。如果你能幫助我,我將不勝感激。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;    }}
查看完整描述

1 回答

?
梵蒂岡之花

TA貢獻1900條經(jīng)驗 獲得超5個贊

也許如果你這樣做


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!!!");

    }


}


查看完整回答
反對 回復(fù) 2024-01-05
  • 1 回答
  • 0 關(guān)注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號